我正在尝试使用反向URL在django中调用应用程序(音乐)中的URL,但无法使用。以下是我的代码
My main_web_app URL file
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^login$/', include('login.urls')),
url(r'^admin/', admin.site.urls),
url(r'^music/', include('Music.urls'), name='indexView')
]
我的音乐/网址文件
from django.conf.urls import url
from . import views
app_name = 'music'
urlpatterns = [
# /music/
url(r'^$', views.IndexView.as_view(), name='indexView'),
# /music/852
url(r'^(?P<album_id>[0-9]+)/$', views.DetailView.as_view(), name='detailsView'),
# /music/add
url(r'^add/$', views.AddAlbum.as_view(), name='addView')
]
我的观看文件
from django.shortcuts import render
from django.views import generic
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from .models import Album
class IndexView(generic.ListView):
template_name = 'Music/index.html'
context_object_name = 'Album_List'
def get_queryset(self):
return Album.objects.all()
class DetailView(generic.DetailView):
template_name = 'Music/details.html'
model = Album
class AddAlbum(CreateView):
model = Album
fields = ['Album_name', 'Artist', 'Genere', 'Album_logo']
我的模板文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<ul>
{% for album in Album_List %}
<li><a href="{% url 'music:detialView' %}">{{ album.Album_name }}
</a></li>
{% endfor %}
</ul>
<a href="{% url 'music:addView' %}" > Add Album</a>
</body>
</html>
即将出现的错误
Error during template rendering
In template C:\Users\Varun\Desktop\newsite\Music\templates\Music\index.html, error at line 10
Reverse for 'detailView' not found. 'detailView' is not a valid view function or pattern name.
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <title>Title</title>
6 </head>
7 <body>
8 <ul>
9 {% for album in Album_List %}
10 <li><a href="{% url 'music:detailView' %}">{{ album.Album_name }}</a></li>
11 {% endfor %}
12 </ul>
13 <a href="{% url 'music:addView' %}" > Add Album</a>
14 </body>
15 </html>
谁能告诉我我做错了什么? 我的应用程序的结构是 网站(主网页应用)==&gt;音乐(包含1个模特(专辑))
答案 0 :(得分:1)
错字在这里。将您的urls.py
更改为:
url(r'^(?P<album_id>[0-9]+)/$', views.DetailView.as_view(), name='detailView'),
请注意s
内的额外detailsView
。