Django Tutorial: "NoReverseMatch" Error

312 views
Skip to first unread message

roflcopterpaul

unread,
Jul 19, 2018, 6:32:14 PM7/19/18
to Django users
Hello, everyone! I have run into a problem I cannot figure out. I have spent days searching online and trying different things in my code, but I cannot figure out where this problem is stemming from. I would truly appreciate any insights.

Admittedly, I am scrub enough that I'm not even sure of all the code I should include, so if I should include anything else, please let me know!

NoReverseMatch at /polls/1/vote/

Reverse for 'results' with arguments '(1,)' not found. 1 pattern(s) tried: ['polls\\/\\<int\\:pk\\/results\\/$']
Request Method:POST
Request URL:http://127.0.0.1:8000/polls/1/vote/
Django Version:2.0.6
Exception Type:NoReverseMatch
Exception Value:
Reverse for 'results' with arguments '(1,)' not found. 1 pattern(s) tried: ['polls\\/\\<int\\:pk\\/results\\/$']
Exception Location:C:\Users\pwalker\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 636
Python Executable:C:\Users\pwalker\AppData\Local\Programs\Python\Python36-32\python.exe
Python Version:3.6.4
Python Path:
['C:\\Users\\pwalker\\Coding\\Python-Practice\\Gaining_Agreement\\GA_site',
 'C:\\Users\\pwalker\\AppData\\Local\\Programs\\Python\\Python36-32\\python36.zip',
 'C:\\Users\\pwalker\\AppData\\Local\\Programs\\Python\\Python36-32\\DLLs',
 'C:\\Users\\pwalker\\AppData\\Local\\Programs\\Python\\Python36-32\\lib',
 'C:\\Users\\pwalker\\AppData\\Local\\Programs\\Python\\Python36-32',
 'C:\\Users\\pwalker\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages']

 views.py
...

def vote(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    try:
        selected_choice = question.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        # Redisplay the question voting formself.
        return render(request, 'polls/detail.html', {'question': question, 'error_message': "You didn't select a choice.", })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        # Always return an HttpResponseRedirect after successfully dealing with POST data.
            # This prevents data from being posted twice if a user hits the Back buttonself.
        return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))

urls.py
urlpatterns = [
    # ex: /polls/
    path('', views.IndexView.as_view(), name='index'),
    # ex: /polls/5/
    # the 'name' value as called by the {% url %} template tag
    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'),
]

templates/polls/results.html  - This is the page it *should* be redirecting to after I click to vote, but that is when it takes me to the error screen.
<h1>{{ question.question_text }}</h1>

<ul>
{% for choice in question.choice_set.all %}
  <li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
{% endfor %}
</ul>

<a href="{% url 'polls:detail' question.id %}">Vote again?</a>


Thank you very much for any help!

Dylan Reinhold

unread,
Jul 19, 2018, 6:35:22 PM7/19/18
to django...@googlegroups.com
You are missing the > in the path
path('<int:pk/results/', views.ResultsView.as_view(), name='results'),
Should be
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/38f2f492-86a3-47e6-9652-cf9a8e858ad6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matthew Pava

unread,
Jul 19, 2018, 6:36:05 PM7/19/18
to django...@googlegroups.com

It looks like you have a typo:

 

urlpatterns = [

    # ex: /polls/

    path('', views.IndexView.as_view(), name='index'),

    # ex: /polls/5/

    # the 'name' value as called by the {% url %} template tag

    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'),

]

 

The red line should probably have this path:

<int:pk>/results/

--

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.

roflcopterpaul

unread,
Jul 19, 2018, 6:40:08 PM7/19/18
to django...@googlegroups.com
Yeah, I just discovered that after posting this. I'm ashamed to admit that single error had me baffled for days.

Thank you so much for the quick reply! 

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/haz9iF_HDpc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Melvyn Sopacua

unread,
Jul 20, 2018, 5:28:14 AM7/20/18
to django...@googlegroups.com
On donderdag 19 juli 2018 20:39:15 CEST roflcopterpaul wrote:
> Yeah, I just discovered that after posting this. I'm ashamed to admit that
> single error had me baffled for days.

No shame needed, because at some point you read over and over it and read the
same wrong thing as right in your brain. There's two tricks to it:
a) put the lines from the example below/above your line and compare above with
below. Obviously this only works when you have an example and works better if
you added or missed a letter.
b) ask sooner. A different set of eyes connected to a fresh brain makes a
world of difference. In fact, a lot of times just composing the mail can solve
a problem as you put the code into a different setting and your brain has to
process it again. Especially if you paste it as plain text without all the
formatting of an IDE/editor.

--
Melvyn Sopacua

roflcopterpaul

unread,
Jul 20, 2018, 6:07:48 AM7/20/18
to django...@googlegroups.com
I truly appreciate the suggestions, Melvyn. Thanks so much!

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/haz9iF_HDpc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages