这是我的views.py
:
from django.http import Http404
from django.shortcuts import render
from .models import Album
def index(request):
all_albums = Album.objects.all()
return render(request, 'music/index.html', {'all_albums': all_albums})
def detail(request, album_id):
try:
album = Album.objects.get(pk=album_id)
except Album.DoesNotExist:
raise Http404("Album does not exist")
return render(request, '/music/detail.html', {'album': album})
这是我的music\urls.py
:
from django.conf.urls import url
from . import views
urlpatterns = (
url(r'^$', views.index, name='index'),
url(r'^(?P<album_id>[0-9]+)/$', views.detail, name='detail'),
)
当我运行此代码时,我收到的错误为:
答案 0 :(得分:1)
由于stacktrace表示您尝试打开http://127.0.0.1/music/id/1
页面而不是http://127.0.0.1/music/1
,但urls.py
中没有此类网址格式。您需要尝试打开http://127.0.0.1/music/1
或添加新模式:
url(r'^id/(?P<album_id>[0-9]+)/$', views.detail, name='detail')
查看http://127.0.0.1/music/id/1
页面。