A_Matar
A_Matar

Reputation: 2330

NoReverseMatch - Django 1.7 Beginners tutorial

I am following the begginers 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 a bit of research I found people asking similar question and someone suggested that they should remove the cash $ from the general url, because the urlloader just takes the empty string, while this doesn't gives me the error No Reverse Match, it messes everything else up, whenever I try to reach any other url it redirects me to the main url, while without removing the cash $ I can perfectly proceed to these urls. So what is it that I am doing wrong?

Here is the project URLS:

urlpatterns = patterns('',
    url(r'^poll/', include('poll.urls', namespace="poll")),
    url(r'^admin/', include(admin.site.urls)),
)

And the app URLS:

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 the 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 guess it might be sth related to how the action {%URL%} and the method post are being passed, so here is the code line from the template file specified 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

Upvotes: 3

Views: 803

Answers (1)

alecxe
alecxe

Reputation: 473993

The URL name in urls.py is votes and you are searching for poll:vote, fix it:

<form action="{% url 'poll:votes' question.id %}" method="post">
                          HERE ^

Upvotes: 4

Related Questions