Django项目:base.html中的链接导致错误
在Django项目中,我有一个指向base.html中指定的“添加相册”的链接代码如下所示Django项目:base.html中的链接导致错误,django,views,render,Django,Views,Render,在Django项目中,我有一个指向base.html中指定的“添加相册”的链接代码如下所示 <ul class="nav navbar-nav navbar-right"> <li class=""> <a href="{% url 'music:album-add' %}"> <span class="glyphicon glyph
<ul class="nav navbar-nav navbar-right">
<li class="">
<a href="{% url 'music:album-add' %}">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add Album
</a>
</li>
from django.views import generic
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from .models import Album
#=============HOME PAGE===================
class IndexView(generic.ListView):
#specify template being used
template_name='music/index.html' #when we get a list of all albums, plug them into this template
context_object_name='all_albums' #if you don't use this variable it is automatically just object_list (which is used in index.html)
#make a query set
def get_queryset(self):
return Album.objects.all()
#=============DETAILS VIEW=============== details about one object
class DetailView(generic.DetailView):
#what model are we looking at /for
model=Album
template_name='music/detail.html'
#===============For the add album form
class AlbumCreate(CreateView):
model=Album
fields=['artist','album_title','genre','album_logo']
template_name='music/album_form.html'
music/views.py文件代码如下
<ul class="nav navbar-nav navbar-right">
<li class="">
<a href="{% url 'music:album-add' %}">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add Album
</a>
</li>
from django.views import generic
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from .models import Album
#=============HOME PAGE===================
class IndexView(generic.ListView):
#specify template being used
template_name='music/index.html' #when we get a list of all albums, plug them into this template
context_object_name='all_albums' #if you don't use this variable it is automatically just object_list (which is used in index.html)
#make a query set
def get_queryset(self):
return Album.objects.all()
#=============DETAILS VIEW=============== details about one object
class DetailView(generic.DetailView):
#what model are we looking at /for
model=Album
template_name='music/detail.html'
#===============For the add album form
class AlbumCreate(CreateView):
model=Album
fields=['artist','album_title','genre','album_logo']
template_name='music/album_form.html'
url.py代码:
ValueError at /music/album-add/
invalid literal for int() with base 10: 'album-add'
Request Method: GET
Request URL: http://127.0.0.1:8000/music/album-add/
Django Version: 2.0
Exception Type: ValueError
Exception Value:
invalid literal for int() with base 10: 'album-add'
Exception Location: C:\Python34\lib\site-packages\django\db\models\fields\__init__.py in get_prep_value, line 947
from django.contrib import admin
from django.urls import include, path
from . import views #the dot means look at the current directory - look for a module called views
app_name='music'
urlpatterns = [
#this is matching /music/
path('', views.IndexView.as_view(), name='index'),
#when you use a detail view it expects a primary key
path("<pk>/", views.DetailView.as_view(), name="detail"),
#/music/album/add - dont need to specify pk
path('album/add/', views.AlbumCreate.as_view(), name="album-add"),
]
来自django.contrib导入管理
从django.url导入包括,路径
从…起导入视图#点表示查看当前目录-查找名为视图的模块
app_name='music'
URL模式=[
#这是配对/音乐/
路径(“”,views.IndexView.as_view(),name='index'),
#使用详图视图时,它需要主键
路径(“/”,views.DetailView.as_view(),name=“detail”),
#/音乐/专辑/添加-无需指定主键
路径('album/add/',views.AlbumCreate.as_view(),name=“album add”),
]
有人能发现错误来解决问题吗?我需要“添加相册”链接才能转到Album_form.html页面。music/templates/music/album\u form.html(其中包含album\u表单,包括表单模板。您的“详细信息”URL模式太笼统,正在捕获所有内容,包括字符串“album add”。您应该做两件事:用
将其约束为整数“/”
,和/或将其移动到专辑添加模式之后。您的如何包含
查找音乐URL?这很有效。谢谢!我仍在努力解决另一个问题,我想这与pk有关。