adirang
adirang

Reputation: 33

Page not found with Django (404)

In my django project, when I access localhost:8000 it says:

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

The urls.py is:

  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")),
  ]

The polls urls.py is:

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

The Django version is 1.10. Can anyone help me identify the problem? Thanks in advance.

Upvotes: 2

Views: 3270

Answers (2)

sreekanth kuriyala
sreekanth kuriyala

Reputation: 1353

In main urls.py change

from

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

to

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

Upvotes: 1

d6bels
d6bels

Reputation: 1482

You do not have a route for / it seems, so http://localhost:8000/ does not get you anywhere.

You wrote url(r'^polls/', include('polls.urls', namespace="polls")), in urls.py so all the routes defined in polls.urls are to be prefixed with polls/.

You may actually want to go to http://localhost:8000/polls/ (notice the polls/, because the route you defined as index is listed in the polls app.

Had you wanted to route your polls index to your url root, you should change urls.py to something like

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

and the forget about the polls/ part in the urls.

Upvotes: 1

Related Questions