NoReverseMatch Exception help in Django
Asked Answered
I

4

10

I'm fairly new to python and following along with part 4 of the tutorial for the Django framework here. I'm trying to implement generic views for the polls app--my code seems correct (as far as I can tell), but when I try to vote, I get a NoReverseMatch Exception that states:

Reverse for 'polls/poll_results' with arguments '(1L,)' and keyword arguments '{}' not found.

My code was working perfectly before I attempted the generic views, but I can't seem pinpoint the problem now.

Here's the code for my urls.py in the poll directory:

from django.conf.urls.defaults import *
from djtest.polls.models import Poll

info_dict = {
    'queryset': Poll.objects.all(),
}

urlpatterns = patterns('',
    (r'^$', 'django.views.generic.list_detail.object_list', info_dict),
    (r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict),
    url(r'^(?P<object_id>\d+)/results/$', 'django.views.generic.list_detail.object_detail', dict(info_dict, template_name='polls/results.html'), 'poll_results'),
    (r'^(?P<poll_id>\d+)/vote/$', 'djtest.polls.views.vote'),
)

And here is the views.py:

from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404
from django.core.urlresolvers import reverse
from djtest.polls.models import Poll, Choice

def vote(request, poll_id):
    p = get_object_or_404(Poll, pk=poll_id)
    try:
        selected_choice = p.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        #redisplay form
        return render_to_response('polls/poll_detail.html', {
            'object': p,
            'error_message': "You didn't select a choice.",
        })
    else:
        selected_choice.votes += 1
        selected_choice.save()       
        return HttpResponseRedirect(reverse('poll_results', args=(p.id,)))

I have a feeling that it is a syntactical error, but I can't find it. Thanks in advance for any help...

Improbity answered 30/7, 2009 at 2:24 Comment(0)
C
5

Try using:

return HttpResponseRedirect(reverse('poll_results', kwargs={'object_id': p.id}))
Commons answered 30/7, 2009 at 3:27 Comment(4)
Thanks--that worked. Could you maybe explain how it differs from the version in the tutorial and why it wasn't working before?Improbity
Don't know about the tutorial. But it seems the generic view is using keyword arguments. The reverse resolver matches args and kwargs separately, so when you were passing args, it didn't match the view that used kwargs. But when you pass kwargs it works. Make sense?Commons
This line was incorrect when I had this problem. The code above only causes an error for me, but the code in the last few lines of the tutorial worked correctly, on my 1.4 installation. What version of Django does this work with?Chary
@NoBugs: Whatever version of Django was current about 3 years ago when I wrote the answer...Commons
A
1

Are you sure that's where your error really is? Based on the error message, it sounds like either in a view or in a template you are trying to reverse 'polls/poll_results' (in a template, you may be doing something like {% url polls/poll_results poll.pk %})

Adai answered 30/7, 2009 at 5:3 Comment(1)
I'm doing something like this in my template but don't understand why it'sKilloran
O
1

I could not find any explanation that fixed the problem, until I ran across this person's abridged Django tutorial: http://tony.abou-assaleh.net/web-development/stripped-down-django-tutorial

It's basically a line in the details template, which should be:

<form action="/polls/{{ poll.id }}/vote/" method="post">

Instead of:

<form action="{% url 'polls.views.vote' poll.id %}" method="post">

I'm not sure why this fixed the issue, but it did for me. I'd love an explanation if anyone has one.

Oath answered 14/8, 2012 at 5:29 Comment(0)
S
0

I've tried the solution provided as answer and didn't worked for me. In my case i was getting the same error (following the same tutorial) and the problem was that the name of the view in the urls.py file was a bit different that in the views.py (because a typing error).

Spree answered 14/1, 2012 at 2:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.