Django 1.8.2 -- Tutorial Chapter 3 -- Error: NoReverseMatch at /polls/ -- Python 3.4.3
Asked Answered
D

0

1

I've been following the official tutorial exactly. It's located here:

https://docs.djangoproject.com/en/1.8/intro/tutorial03/

It's been going well, but suddenly I'm getting this error:

Reverse for 'detail' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []

It happened as soon as I changed this line:

<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>

to this new version:

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

That line of code is located in the following index.html file (I'm not sure how to show the proper indententation on stackoverflow):

{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
    <li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

Here is the detail.html file:

<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
    <li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>

Here is the polls/views.py file:

from django.shortcuts import get_object_or_404, render
from .models import 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):
    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)

Here is the polls/urls.py file:

from django.conf.urls import url
from . import views
urlpatterns = [
    # ex: /polls/
    url(r'^$', views.index, name='index'),
    # ex: /polls/5/
    url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
    # ex: /polls/5/results/
    url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
    # ex: /polls/5/vote/
    url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
] 

Here is the mysite/urls.py file:

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^polls/', include('polls.urls', namespace="polls")),
    url(r'^admin/', include(admin.site.urls)),
]

Here is the polls/models.py file:

import datetime
from django.db import models
from django.utils import timezone

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __str__(self):              # __unicode__ on Python 2
        return self.question_text
    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
    was_published_recently.admin_order_field = 'pub_date'
    was_published_recently.boolean = True
    was_published_recently.short_description = 'Published recently?'

class Choice(models.Model):
    question = models.ForeignKey(Question)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
    def __str__(self):              # __unicode__ on Python 2
        return self.choice_text

How can I fix the error? Any advice would be much appreciated. I am an absolute beginner with Django and Python, though I do have a little php experience.

Thanks.

Directional answered 28/6, 2015 at 19:35 Comment(2)
You skipped ahead to the last paragraph? If you use namespace="polls" in your urls.py, you need to use {% url 'polls:detail' question.id %} in your template.Callboard
Yes that works. If you post it as an answer, I will mark it as the correct one. Thanks for the quick reply.Directional

© 2022 - 2024 — McMap. All rights reserved.