Django没有找到页面(404)

时间:2017-04-25 16:06:31

标签: python django

在我的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。任何人都可以帮我识别问题吗?提前谢谢。

2 个答案:

答案 0 :(得分:1)

您似乎没有/的路线,因此http://localhost:8000/无法让您到达任何地方。

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

您可能真的想转到http://localhost:8000/polls/(请注意民意调查/ ,因为您定义为索引的路线列在民意调查应用中。

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

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

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

答案 1 :(得分:1)

在主要的urls.py更改

来自

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

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