getting 404 page not found on part 3 of the tutorial

24 views
Skip to first unread message

George P. Olson

unread,
Nov 11, 2020, 7:49:05 PM11/11/20
to django...@googlegroups.com
Hi guys,
New here to django. Going through the tutorial, on part 3, I set up the mysite/polls/urls.py file  like this according to the instructions:

-----
from django.urls import path
from . import views

urlpatterns = [
    # ex: /polls/
    path('', views.index, name='index'),
    # ex: /polls/5/
    path('<int:question_id>/', views.detail, name='detail'),
    # ex: /polls/5/results/
    path('<int:question_id>/results/', views.results, name='results'),
    # ex: /polls/5/vote/
    path('<int:question_id>/vote/', views.vote, name='vote'),
]
-----

Then I set up the /mysite/polls/views.py file like this according to the instructions:

-----
from django.http import HttpResponse
from .models import Question

def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    output = ', '.join([q.question_text for q in latest_question_list])
    return HttpResponse(output)

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

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

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

So when I go to this url: http://127.0.0.1:8000/polls/

I get this:

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:

  1. admin/
  2. polls [name='index']
  3. polls <int:question_id>/ [name='detail']
  4. polls <int:question_id>/results/ [name='results']
  5. polls <int:question_id>/vote/ [name='vote']

The current path, polls/, didn't match any of these.


I cannot see what the error is that I am making. Any ideas? Thanks in advance.

Clive Bruton

unread,
Nov 11, 2020, 8:47:58 PM11/11/20
to django...@googlegroups.com

On 11 Nov 2020, at 23:37, George P. Olson wrote:

> So when I go to this url: http://127.0.0.1:8000/polls/

Although the urlpatterns below shows "# ex: /polls/", the defined
path has no path elements, so you just need:

http://127.0.0.1:8000/

I think.

> urlpatterns = [
> # ex: /polls/
> path('', views.index, name='index'),...


-- Clive

George Olson

unread,
Nov 11, 2020, 11:35:12 PM11/11/20
to Django users
I don't quite follow your note there of just going to:
without the app name 'polls' following.

I believe you have to put the app name following that. It was working before with http://127.0.0.1:8000/polls/, but then I did something and tried to backtrack and the whole system got messed up. I tried to reset everything back to the original but it is still not working.

Clive Bruton

unread,
Nov 12, 2020, 12:10:21 AM11/12/20
to django...@googlegroups.com
Sorry, I just checked my version of the tutorial, you load the
manage.py through the directory "mysite", and "polls" is in a sub-
directory. The way you showed the "/polls/urls.py" implied that was
the only file, where in fact the root app/settings are at "/mysite/
settings.py" and urls at "/mysite/urls.py".

Because "/mysite/urls.py" has an include for "polls/urls.py" it
implies the path "/polls".

It's a bit tricky to diagnose, because you are now saying it was
working, but then you changed stuff. My directory setup looks like this:

/mysite/manage.py
...
/mysite/mysite/settings.py
...
/mysite/mysite/urls.py
...
/mysite/polls/settings.py
...
/mysite/polls/urls.py

I checked my polls.py, which looks like this:

*************

from django.urls import path

from . import views

app_name = 'polls'

"""
urlpatterns = [
# ex: /polls/
path('', views.index, name='index'),
# ex: /polls/5/
path('<int:question_id>/', views.detail, name='detail'),
# ex: /polls/5/results/
path('<int:question_id>/results/', views.results, name='results'),
# ex: /polls/5/vote/
path('<int:question_id>/vote/', views.vote, name='vote'),
]
"""

urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('<int:pk>/results/', views.ResultsView.as_view(),
name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]

***************

I think I read recently that generic views like "IndexView" need a
".as_view()" attached to the end to be recognised.


-- Clive
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to django-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/
> d/msgid/django-users/1feb0c4a-8919-4aff-abb1-e0110392c396n%
> 40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages