Tales Pádua
Tales Pádua

Reputation: 1461

NoReverseMatch error on DjangoCMS Tutorial: Plugins

I am doing the DjangoCMS tutorial: http://django-cms.readthedocs.org/en/latest/introduction/plugins.html

Everything was good until this point, but I get the following error when I try to add the Poll Plugin to some placeholder:

Reverse for 'vote' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['en/polls/(?P<poll_id>\\d+)/vote/$']

The template:

<h1>{{ instance.poll.question }}</h1>
<form action="{% url 'polls:vote' instance.poll.id %}" method="post">
{% csrf_token %}
<div class="form-group">
    {% for choice in instance.poll.choice_set.all %}
        <div class="radio">
            <label>
                <input type="radio" name="choice" value="{{ choice.id }}">
                {{ choice.choice_text }}
            </label>
        </div>
    {% endfor %}
</div>
<input type="submit" value="Vote" />

the view:

class IndexView(generic.ListView):
    template_name = 'polls/index.html'
    context_object_name = 'latest_poll_list'

    def get_queryset(self):
        return Poll.objects.all()[:5]


class DetailView(generic.DetailView):
    model = Poll
    template_name = 'polls/detail.html'


class ResultsView(generic.DetailView):
    model = Poll
    template_name = 'polls/results.html'


def vote(request, poll_id):
    p = get_object_or_404(Poll, pk=poll_id)
    try:
        selected_choice = p.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
    # Redisplay the poll voting form.
        return render(request, 'polls/detail.html', {
            'poll': p,
            'error_message': "You didn't select a choice.",
        })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        return HttpResponseRedirect(reverse('polls:results', args=(p.id,)))

The error is happening in the line <form action="{% url 'polls:vote' 'instance.poll.id' %}" method="post">

The urls.py of the poll app:

urlpatterns = patterns('',
    url(r'^$', views.IndexView.as_view(), name='index'),
    url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
    url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'),
    url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),)

The migration goes ok and the Poll Plugin shows on the plugin lists, even the popup to select the plugin object open nice. When I add the plugin and confirm, the webpage crashes. To get the website to open again, I need to manually delete the page on /admin.

I also tried to put instance.poll.id inside single quotes, but I got the same error. Please help me. Thanks!

Upvotes: 2

Views: 303

Answers (1)

sogeking
sogeking

Reputation: 1276

This must be because you are trying to display a link to an non-existing poll instance. I mean, in your template:

<form action="{% url 'polls:vote' instance.poll.id %}" method="post">

I bet that your instance.poll.id is None, thus django fails to find any suitable urlconf (as you can see, r'^(?P\d+)/vote/$' asks for at least one digit as as parameter). As a test: can you try once again, by commenting out that line and displaying just {{ instance.poll }} ? Does it show anything at all?

Solution: before trying to display the plugin, you must set a valid value to instance.poll.

Upvotes: 1

Related Questions