Ajay Shah
Ajay Shah

Reputation: 414

Django Page not found (404) polls:detail url issue

While at Django tutorial, it asks us to tweak the view to generic views. Post that, I am able to click on my question "What's up" at index.html but it leads to a page not found error. Below is the code:

urls.py(for polls)

from django.urls import path

from . import views

app_name='polls'

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

index.html

{% load static %}

<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}">

{% 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 %}

views.py

from django.shortcuts import render,get_object_or_404
from django.http import Http404,HttpResponseRedirect
from django.urls import reverse
from django.views import generic


# Create your views here.
#from django.http import HttpResponse
from django.template import loader
from .models import Question, Choice

class IndexView(generic.ListView):
    template_name = 'polls/index.html'
    context_object_name = 'latest_question_list'

    def get_queryset(self):
        """Return the last five published questions."""
        return Question.objects.order_by('-pub_date')[:5]

class DetailView(generic.DetailView):
    model = Question
    template_name = 'polls/detail.html'

class ResultsView(generic.DetailView):
    model = Question
    template_name = 'polls/results.html'

When I click on the link "What's up?" at index.html, it leads me to the following error:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/polls/%7B%25%20url%20'polls:detail'%20question.id%20%25
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']
admin/
The current path, polls/{% url 'polls:detail' question.id %, 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 come from a Bottle background and learning Django and found it really well organized except for this hiccup.

I have tried changing the question.id to question.pk. Also, I tried changing quotation marks around the 'polls:detail' and that also didn't help.

Upvotes: 0

Views: 141

Answers (1)

Alasdair
Alasdair

Reputation: 308999

You have missed the trailing } at the end of your {% url %} tag in index.html. It should be:

 <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>

Upvotes: 1

Related Questions