NoReverseMatch Tutorial - Django 1.7 Beginners
I am following a beginner tutorial in Django 1.7.1 and am getting this error
Reverse for 'vote' with arguments '(5,)' and keyword arguments '{}' not found. 0 pattern(s) tried: [] `poll\templates\poll\detail.html, error at line 12`
after doing a little research, I found that people are asking a similar question and someone suggested that they remove the cash $
from the generic url because the urlloader just takes an empty string, while that doesn't give me the No Reverse Match error. it messes things up, when i try to reach any other url, it redirects me to the main url without charging any cash $
, i might well navigate to those urls. So what am I doing wrong?
Here is the url of the project:
urlpatterns = patterns('',
url(r'^poll/', include('poll.urls', namespace="poll")),
url(r'^admin/', include(admin.site.urls)),
)
And the app url:
from django.conf.urls import patterns, url
from poll import views
urlpatterns = patterns('',
#e.g. /poll/
url(r'^$', views.IndexView.as_view(), name='index'),
#e.g. /poll/5/
url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
#e.g. /poll/5/results/
url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'),
#e.g. /poll/5/votes/
url(r'^(?P<question_id>\d+)/votes/$', views.votes, name='votes'),
)
And views:
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views import generic
from poll.models import Question, Choice
class IndexView(generic.ListView):
template_name = 'poll/index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
"""Return the last five published questions."""
return Question.objects.order_by('-pup_date')[:5]
class DetailView(generic.DetailView):
model = Question
template_name = 'poll/detail.html'
class ResultsView(generic.DetailView):
model = Question
template_name = 'poll/results.html'
def votes(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):
# Redisplay the question voting form.
return render(request, 'poll/detail.html', {
'question': p,
'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 button.
return HttpResponseRedirect(reverse('poll:results', args=(p.id,)))
Also I am guessing it might have something to do with how the action {%URL%}
and method are passed post
, so here is the line of code from the template file mentioned in the error<form action="{% url 'poll:vote' question.id %}" method="post">
Please let me know if you need anything else and thanks in advance
source to share