I am trying to complete the Django tutorial: Writing your first Django app. I am currently stuck on part 4.
After updating my template polls/templates/polls/detail.html
, rewriting my polls/views.py
and creating a results.html
located at polls/templates/polls/results.html
, I ran the server and tried to go to http://127.0.0.1:8000/polls/1/, but I get this Error:
Page not found (404) No Question matches the given query. Request Method: GET Request URL: http://127.0.0.1:8000/polls/1/ Raised by: polls.views.detail Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order: polls/ [name='index'] polls/ <int:question_id>/ [name='detail'] The current path, polls/1/, 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.
I see that the error is raised by the html-file detail
which looks like this:
<form action="{ url 'polls:vote' question.id }" method="post"> { csrf_token } <fieldset> <legend><h1>{{ question.question_text }}</h1></legend> { if error_message }<p><strong>{{ error_message }}</strong></p>{ endif } { for choice in question.choice_set.all } <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}"> <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br> { endfor } </fieldset> <input type="submit" value="Vote"> </form>
My other two templates (html-files) index
and result
looks like this:
index.html:
{ if latest_question_list } <ul> { for question in latest_question_list } <li><a href="{ url 'polls:detail' question.id }">{{ question.question_text }}</a></li> { endfor } </ul> { else } <p>No polls are available.</p> { endif }
results.html:
<h1>{{ question.question_text }}</h1> <ul> { for choice in question.choice_set.all } <li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li> { endfor } </ul> <a href="{ url 'polls:detail' question.id }">Vote again?</a>
When I try to run just http://127.0.0.1:8000, I get this Error Message:
Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/ Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order: polls/ admin/ The empty path didn’t match any of these. 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.
I tried to re-do the steps mentioned above (the first three steps of Part 4), making sure I use the correct paths, but I still get errors.
This is how my .py’s look:
…pollsviews.py :
from django.http import HttpResponseRedirect from django.shortcuts import render, get_object_or_404 from django.urls import reverse from .models import Choice, Question def index(request): latest_question_list = Question.objects.order_by('-pub_date')[:5] context = {'latest_question_list': latest_question_list} return render(request, 'polls/index.html', context) def detail(request, question_id): question = get_object_or_404(Question, pk=question_id) return render(request, 'polls/detail.html', {'question': question}) def results(request, question_id): question = get_object_or_404(Question, pk=question_id) return render(request, 'polls/results.html', {'question': question}) def vote(request, question_id): question = get_object_or_404(Question, pk=question_id) try: selected_choice = question.choice_set.get(pk=request.POST['choice']) except (KeyError, Choice.DoesNotExist): return render(request, 'polls/detail.html', { # Redisplay the question voting form 'question': question, 'error_message': "You didn't select a choice" }) else: selected_choice.votes += 1 selected_choice.save() # Always return an HttpResponseRedirect after successfully dealing with POST data. # This prevents data from being posted twice if a user hits the Back button. return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
…/polls/urls.py:
from django.urls import path from . import views app_name = 'polls' urlpatterns = [ path('', views.index, name='index'), # ex: /polls/ path('<int:question_id>/', views.detail, name='detail'), # ex: /polls/5/ path('<int:question_id>/results/', views.results, name='results'), # ex: /polls/5/results/ path('<int:question_id>/vote/', views.vote, name='vote'), # ex: /polls/5/vote/ ]
I have already looked at these questions:
Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/
Page not found 404 on Django site?
Django Http404 message not showing up
Django Page not found(404) error (Library not found)
Django 404 error-page not found
(and many more) trying to figure this out for two days, but I can’t seem to figure it out. I realize that this question probably is a duplicate, but I’m desperate, and I appreciate any help I can get.
Advertisement
Answer
So this isn’t a problem with any of your HTML files or the URLs file. The clue to that can be found in the error message, here:
Raised by: polls.views.detail
Which indicates that detail returns a 404 object. If we look at that part of views, you have this line:
question = get_object_or_404(Question, pk=question_id)
My guess is that you don’t have a question in the database with the ID 1 OR the database is empty. If either of those are true, get_object_or_404 would return a 404.
If there was an issue with the page actually not being found, you would not see the “Raised by” part of the error at all.