django - NoReverseMatch at /polls/1/vote/ -
there 3 question on stack overflow different error.in mine argument being passed don't seem figure out error is?
noreversematch @ /polls/1/vote/ reverse 'result' arguments '(1,)' , 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 'result' arguments '(1,)' , 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 polls/views.py
from django.shortcuts import render, get_object_or_404 django.http import httpresponse, http404, httpresponseredirect django.core.urlresolvers import reverse .models import movie, rating # create 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 rating",}) else: selected_rating.votes += 1 selected_rating.save() return httpresponseredirect(reverse('polls:result', args=(p.id,)))
please ask me if need additional file put them here.
here 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 %} {% 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 result.html
<h1>{{movie.movie_text}}</h1> <ul> {% 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 polls/urls.py
from django.conf.urls import url . 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 mysite/urls.py
from django.conf.urls import include, url django.contrib import admin urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^polls/', include('polls.urls', namespace='polls')), ]
you haven't got view named "polls:result", have 1 named "polls:results". either change name in polls/urls.py, or value use in call reverse
in vote
view.
Comments
Post a Comment