Reputation: 394
There are three question on Stack overflow with slightly different error.In mine argument is also being passed but I just don't seem to figure out what the error is?
NoReverseMatch at /polls/1/vote/
Reverse for 'result' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Request Method: POST
Request URL: http://localhost:8000/polls/1/vote/
Django Version: 1.8.2
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'result' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Exception Location: C:\Python27\Lib\site-packages\django\core\urlresolvers.py in _reverse_with_prefix, line 496
Python Executable: C:\Python27\python.exe
Here is my polls/views.py
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.core.urlresolvers import reverse
from .models import Movie, Rating
# Create your views here.
def detail(request, movie_id):
try:
movie = get_object_or_404(Movie,pk = movie_id)
except Movie.DoesNotExist:
raise Http404("Movie not found")
return render(request, 'polls/detail.html', {'movie': movie})
def result(request, movie_id):
movie = get_object_or_404(Movie, pk = movie_id)
return render(request, 'polls/result.html', {'movie': movie})
def vote(request, movie_id):
p = get_object_or_404(Movie, pk = movie_id)
try:
selected_rating = p.rating_set.get(pk=request.POST['rating'])
except(KeyError, Rating.DoesNotExist):
return render(request, 'polls/detail.html', {'movie':p, 'error_message':"you didn't select a rating",})
else:
selected_rating.votes += 1
selected_rating.save()
return HttpResponseRedirect(reverse('polls:result', args=(p.id,)))
Please ask me if you need any additional file I will put them here.
Here is detail.html
<h1> {{movie.movie_text}}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'polls:vote' movie.id %}" method="post">
{% csrf_token %}
{% for rating in movie.rating_set.all %}
<input type="radio" name="rating" id="rating{{ forloop.counter }}" value="{{ rating.id }}" />
<label for="rating{{ forloop.counter }}">{{ rating.rating_number }}</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>
and here is result.html
<h1>{{movie.movie_text}}</h1>
<ul>
{% for rating in movie.rating_set.all%}
<li>{{rating.rating_text}} -- {{rating.votes }} vote{{rating.votes|pluralize}}</li>
{% endfor %}
</ul>
<a href="{% url 'polls:detail' movie.id %}">Vote Again? </a>
Here is polls/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
#ex: /polls/
url(r'^$', views.index, name = 'index'),
#ex /polls/5/
url(r'^(?P<movie_id>[0-9]+)/$', views.detail, name='detail'),
url(r'^(?P<movie_id>[0-9]+)/result/$', views.result, name='results'),
url(r'^(?P<movie_id>[0-9]+)/vote/$', views.vote, name='vote')
]
Here is mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^polls/', include('polls.urls', namespace='polls')),
]
Upvotes: 2
Views: 2669
Reputation: 11
In my case, I got this error cause I was not returning redirect instead I was trying to render the 'detail.html' template when the post was successful
Upvotes: 0
Reputation: 599648
You haven't got a view named "polls:result", you have one named "polls:results". Either change the name in polls/urls.py, or the value you use in the call to reverse
in the vote
view.
Upvotes: 1