Django Tutorial - 404 Poll Error While Viewing Details

I follow https://docs.djangoproject.com/en/1.8/intro/tutorial03/ I configured the project directory in the same way as in the tutorial. I set up polling urls. According to the tutorial, I determined the details, the results, voted in views.py and polled urls in urls.py polls, which are defined in the urls.py project directory. When do I access

local: 8000 / polls /

it works. But when I try to access the details of the survey, as they described it, he throws 404. I tried my current questions, but all in vain.
In Docs, they say “at” / polls / 34 /. ”Itll launches the detail () method, but for me it throws 404

local: 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 my urls.py is 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 are my urls.py polls 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 are my survey views

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 not find what is wrong with my code. Help me please?

+4
source share
1 answer

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

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

Instead:

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

Hope this helps! Check out the training page :)

+7
source

Source: https://habr.com/ru/post/1607848/


All Articles