在我的django项目中,当我访问localhost:8000时,它说:

  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。谁能帮我找出问题所在?提前致谢。

最佳答案

您似乎没有前往/的路线,因此http://localhost:8000/不会带您到任何地方。

您在url(r'^polls/', include('polls.urls', namespace="polls")),中写了urls.py,因此在polls.urls中定义的所有路由都必须以polls/为前缀。

您可能实际上想转到http://localhost:8000/polls/(请注意polls /,因为在polls应用程序中列出了定义为索引的路由。

如果您想将民意调查索引路由到URL根目录,则应将urls.py更改为

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


忘记了网址中的polls/部分。

10-07 17:17