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/
From: [email protected] [mailto:[email protected]] On
Behalf Of roflcopterpaul
Sent: Thursday, July 19, 2018 1:18 PM
To: Django users
Subject: Django Tutorial: "NoReverseMatch" Error
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!
--
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
[email protected]<mailto:[email protected]>.
To post to this group, send email to
[email protected]<mailto:[email protected]>.
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<https://groups.google.com/d/msgid/django-users/38f2f492-86a3-47e6-9652-cf9a8e858ad6%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.
--
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 [email protected].
To post to this group, send email to [email protected].
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/668d07e3dacb45ecb71559ea4979631c%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.