2016-12-28 162 views
0

這是我views.pyURL錯誤,找不到網頁404

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'), 
) 

當我運行這段代碼,我得到的錯誤爲:

error image

+0

您在瀏覽器中輸入了哪個網址? –

+0

也添加項目的'urls.py'。 – neverwalkaloner

+0

從django.conf.urls導入包括,URL 從django.contrib中導入管理 urlpatterns的= [ URL(R '^管理員/',admin.site.urls) URL(R '^音樂/',包括('music.urls')), ] 這是項目urls –

回答

1

正如堆棧跟蹤說你是@試圖打開http://127.0.0.1/music/id/1頁面不是http://127.0.0.1/music/1但你的urls.py沒有這樣的urp模式。你需要嘗試打開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頁。