2017-04-25 68 views
2

在我的Django項目,當我訪問本地主機:8000,它說:頁面未使用Django發現(404)

Page not found (404) 
    Request Method: GET 
    Request URL: http://localhost:8000/ 

的urls.py是:

from django.conf.urls import include, url 
    from django.contrib import admin 
    from polls import views 

    urlpatterns = [ 
     url(r'^admin/', admin.site.urls), 
     url(r'^polls/', include('polls.urls', namespace="polls")), 
    ] 

民意調查的urls.py是:

from django.conf.urls import url, include 
    from django.contrib import admin 
    from polls import views 

    urlpatterns = [ 
     url(r'^$', views.index, name='index'), 
     url(r'^(?P<question_id>\d+)/$', views.detail, name='detail'), 
     url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'), 
     url(r'^(?P<question_id>\d+)/results/$', views.results, name='results'), 
     url(r'^admin/', admin.site.urls), 
    ] 

Django版本是1.10。任何人都可以幫我找出問題嗎?提前致謝。

回答

1

你不必爲/它似乎是一個路線,所以http://localhost:8000/不會讓你在任何地方。

您在urls.py寫道url(r'^polls/', include('polls.urls', namespace="polls")),因此所有在polls.urls定義的路線要與polls/前綴。

你實際上可能要到http://localhost:8000/polls/(注意民調/,因爲你定義爲指標的路徑在民意調查中的應用程序中列出。

假如你想航線的投票指數您的網址根,你應該改變urls.py到類似

urlpatterns = [ 
    url(r'^', include('polls.urls', namespace="polls")), 
    url(r'^admin/', admin.site.urls), 
] 

和忘記在URL中polls/一部分。

1

在主urls.py從

url(r'^polls/', include('polls.urls', namespace="polls")), 

改變

url(r'^$', include('poll.urls')),