Brandon
Brandon

Reputation: 75

Django 404 Error page not found...the current path matched the last one

I'm new to django and I'm playing around with my own variation of the polls tutorial. My app was working fine, every page loaded correctly. Then I made a single change; I created and then deleted a model. Now, many of urls seem broken. And I don't understand this error:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/polls/1/pick/
Raised by:  polls.views.pick
Using the URLconf defined in mySite.urls, Django tried these URL patterns, in this order:

polls/ [name='index']
polls/ <int:pk>/ [name='detail']
polls/ <int:pk>/results/ [name='results']
polls/ <int:question_id>/vote/ [name='vote']
polls/ <int:question_id>/tally/ [name='tally']
polls/ <int:question_id>/pick/ [name='pick']
The current path, polls/1/pick/, matched the last one.

You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

So I don't understand why it says both that: The current path, polls/1/pick/, matched the last one.

and also page not found? How does that work? And what could have caused this when the app was working fine previously?

My urls.py:

from django.urls import path

from . import views

app_name = 'polls'

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'),
    path('<int:question_id>/tally/', views.tally, name='tally'),
    path('<int:question_id>/pick/', views.pick, name='pick'),
    path('<int:pk>/video/', views.VideoView.as_view(), name='video'),
    path('<int:pk>/reveal/', views.RevealView.as_view(), name='reveal'),
]

And views snippit:

def pick(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    player = get_object_or_404(Player, pk=1)
    question.score = player.score
    question.save()
    return HttpResponseRedirect(reverse('polls:video', args=(question.id,)))

Upvotes: 4

Views: 6240

Answers (1)

Brad Martsberger
Brad Martsberger

Reputation: 1967

This line of the error

Raised by:  polls.views.pick

Is telling you that you didn't get a 404 due to not finding a matching URL, you got a 404 because your view function polls.views.pick raised a 404. You have two lines in that function that could raise a 404

question = get_object_or_404(Question, pk=question_id)
player = get_object_or_404(Player, pk=1)

So in your database, you either don't have a Question with pk=1 or you don't have a Player with pk=1.

Upvotes: 9

Related Questions