Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Tutorial - Poll Error 404 when viewing details

I am following https://docs.djangoproject.com/en/1.8/intro/tutorial03/ I have configured my project directory the way in it is done in tutorial. I was configuring urls for polls. According to tutorial i have defined detail,results,vote in views.py and poll urls in urls.py of polls which is defined in project directory's urls.py. When I access

localhost:8000/polls/

it works. But when i try to access details of a poll as they described it throws a 404.I tried my actual question ids but all in vain.
In Docs they say " at “/polls/34/”. It’ll run the detail() method" But for me it throws 404

localhost:8000/polls/34/

Using the URLconf defined in DjangoFirst.urls, Django tried these URL patterns, in this order:
^polls ^$ [name='index']
^polls ^(?P<question_id>[0-9]+)/$ [name='detail']
^polls ^(?P<question_id>[0-9]+)/results/$ [name='results']
^polls ^(?P<question_id>[0-9]+)/votes/$ [name='vote']
^admin/
The current URL, polls/34/, didn't match any of these.

Here's my urls.py located in ProjectName/ProjectName/urls.py

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

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

]

Here's my polls urls.py located in ProjectName/polls/urls.py

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
    url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
    url(r'^(?P<question_id>[0-9]+)/votes/$', views.vote, name='vote'),
]

Here's my polls view

from django.http import HttpResponse


def index(request):
    return HttpResponse("Hello World")

def detail(request, question_id):
    return HttpResponse("You are looking at question %s." % question_id)

def results(request,  question_id):
    response =  "You are looking at response of question %s."
    return HttpResponse(response % question_id)

def vote(request, question_id):
    return HttpResponse("You are voting on question %s." % question_id)

I can't find what is wrong with my code. Help Me Please?

like image 801
Ryan Cyrus Avatar asked Mar 14 '23 18:03

Ryan Cyrus


1 Answers

I think it's because it should be (in your code it's missing a /):

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

Instead of:

url (r'^polls', include('polls.urls')),

Hope this helps! Check the tutorial page :)

like image 70
jlnabais Avatar answered Mar 23 '23 04:03

jlnabais