NoReverseMatch at /polls/1/results/

712 views
Skip to first unread message

H M

unread,
May 7, 2015, 3:09:49 AM5/7/15
to django...@googlegroups.com
I am on part 4 django tutorial. The tutorial is very good and I easily get on part 4 but....

I get error: Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/(?P<question_id>\\d+)/$']

Am I missing something?

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': "Nijesi izabrao pitanje.",
         })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        return HttpResponseRedirect(reverse('polls:results', args=(p.id,)))





James Schneider

unread,
May 7, 2015, 5:10:47 AM5/7/15
to django...@googlegroups.com

I'm guessing the issue is actually in your template if you are following along that page. None of the code in your views would generate that error from what I can see.

The error indicates that you are trying to reverse('polls:detail') and not providing any arguments for the URL resolver (or are passing an empty/missing variable). In the template, this would probably look something like {% url 'polls:detail' %}. I would guess that you are forgetting question.id after the view name, assuming you are using the same variable names as the tutorial.

Check near the bottom of the stack trace on the page, I bet it has a {% url %} tag highlighted as the culprit.

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2e2320f1-6813-4e01-8b32-76694dbd22f8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Luis Zárate

unread,
May 8, 2015, 10:41:49 PM5/8/15
to django...@googlegroups.com
Which urls.py you paste here? The project URLs or the app urls .?

It is because you are using namespace in the url reverse so you need to named in your project's URLs and put the code paste here in your app urls.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciUv65UQLmJKjWwySKga08gvAa0OHwkZ39hOj4CjPBS3ww%40mail.gmail.com.

> For more options, visit https://groups.google.com/d/optout.
>

--
"La utopía sirve para caminar" Fernando Birri



Luis Zárate

unread,
May 8, 2015, 10:50:30 PM5/8/15
to django...@googlegroups.com

Muhammad M

unread,
May 8, 2015, 11:11:05 PM5/8/15
to django...@googlegroups.com

HM,

Upon a closer look at your code (views.results), you have this:

return render(request, 'polls/results.html', {'guestion': question})

You are passing "guestion" (with a G) to the template instead of "question" (with a Q).

As such, your template complains when you try to use "question.id" because it is expecting "guestion.id" (with a G).

Change it to {'question': question} (with a Q) in views.results and see how it goes again.

All the best. :)

Sincerely,
Muhammad

James Schneider

unread,
May 9, 2015, 12:05:00 AM5/9/15
to django...@googlegroups.com

I agree that the typo is also an issue and should be fixed, but that wouldn't result in the OP's error, since reverse() is complaining about a 'detail' URL specifically. The typo would result in a similar error when the result page is displayed, and would show 'guestion' as one of the kwargs.

-James

Daniel Roseman

unread,
May 9, 2015, 6:39:04 AM5/9/15
to django...@googlegroups.com
On Saturday, 9 May 2015 05:05:00 UTC+1, James Schneider wrote:

I agree that the typo is also an issue and should be fixed, but that wouldn't result in the OP's error, since reverse() is complaining about a 'detail' URL specifically. The typo would result in a similar error when the result page is displayed, and would show 'guestion' as one of the kwargs.

-James 

No, it wouldn't, and the typo is the cause of the error. 

The error occurs when the results page is rendered. That page has a link to the question. The url tag is trying to render that link: `{% url "detail" question_id=question.id %}`. But there is no `question` variable in the context, only `guestion`, so the question_id parameter to that URL tag is blank. Hence the error.
--
DR.

James Schneider

unread,
May 9, 2015, 6:01:52 PM5/9/15
to django...@googlegroups.com
Yep, you're right. I hadn't thought far enough back in my own train of thought. For some reason I had assumed that the OP just had {% url 'detail' %} somewhere in a template (probably because I had just run into a similar situation on a project), forgetting that the template system throws out invalid context variable references. 

Thanks for the correction.

-James

Reply all
Reply to author
Forward
0 new messages