user1406647
user1406647

Reputation: 497

Do I need render(request...) or HttpResponseRedirect(reverse...) in djangoproject tutorial 4

In djangoproject tutorial part 4 i get following error:

NoReverseMatch at /polls/1/results/ Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/(?P\d+)/$']

1   <h1>{{ question.question_text }}</h1>
2   
3   <ul>
4   {% for choice in question.choice_set.all %}
5       <li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
6   {% endfor %}
7   </ul>
8   
9   <a href="{% url 'polls:detail' question.id %}">Vote again?</a>
10  

It seems that I wrote exactly what is in tutorial, does anyone have a clue what could be wrong?

I tried other approaches from stackedoverflow like: Django 1.7.4: Error on Django 1.7 tutorial04: NoReverseMatch error for Reverse for 'vote'

http://djangotalk.blogspot.com/2014/12/re-reverse-for-results-with-arguments_2.html

but all those errors could point to different things

My code is following:

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^(?P<question_id>\d+)/$', views.detail, name='detail'),
    url(r'^(?P<question_id>\d+)/results/$',views.results, name='results'),
    url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),

views.py:

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.template import RequestContext, loader
from polls.models import Question, Choice
from django.http import Http404
from django.core.urlresolvers import reverse


def index(request):
    latest_question_list =  Question.objects.order_by('-pub_date')[:5]
    template = loader.get_template('polls/index.html')
    context = RequestContext(request, {'latest_question_list' : latest_question_list, })
    return HttpResponse(template.render(context))

def detail(request, question_id):
    try:
        question = Question.objects.get(pk = question_id)
    except Question.DoesNotExist:
        raise Http404("Question ne postoji")
    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', {'guestion': question})


def vote(request, question_id):
    p = get_object_or_404(Question, pk=question_id)
    try:
        selected_choice  = p.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        return render(request, 'polls/detail.html', {
            'question': p,
            'error_message': "Did not choose answer.",
         })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        return HttpResponseRedirect(reverse('polls:results', args=(p.id,)))

Upvotes: 1

Views: 853

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599610

Your results view has a typo:

return render(request, 'polls/results.html', {'guestion': question})
                                               ^ g instead of q

Because of that, your template has no question value, so the reverse URL resolution fails - you can see that the error message says it is passing blank values for the id, instead of the value of question.id.

Upvotes: 1

Related Questions