1

I'm following the Django tutorial. I've reached part 4, at the moment of building the vote form. Sadly, I cannot find the problem that is causing the following error:

NoReverseMatch at /polls/1/vote/

Reverse for 'vote' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/(?P[0-9]+)/vote/$']

Both /polls/1/vote and /polls/1/ throw the error above.

My polls/urls.py

from django.conf.urls import url

from . import views

app_name = 'polls'
urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^(?P<question_id>[0-9]+)/$', views.details, name='details'),
    url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
    url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]

And here's the details.html, which shows the error at line 11, the form opening tag:

<h1>{{ question.question }}</h1>

{% if error %}
    <p>
    <strong>
        {{ error  }}
    </strong>
    </p>
{% endif %}

<form action="{% url 'polls:vote' question_id %}" method="POST">
    {% csrf_token  %}
    {% for choice in question.choice_set.all %}
    <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}"/>
    <label for="choice{{ forloop.counter }}">{{ choice.choice }}</label>
    <br/>
    {% endfor %}
    <input type="submit" value="Vote">
</form> 
  • Does `polls/1/results` works fine? – Gocht Mar 15 '16 at 21:01
  • Could you also post the main urls.py file – anonDuck Mar 15 '16 at 21:04
  • As the error `Reverse for 'vote' with arguments '('',)' and keyword arguments '{}' not found` show both arguments and keyword arguments are empty. That means `question_id` is not present in context or might be just empty string. Are you sure `question_id` it is in the template context and contains an integer value? – Aamir Rind Mar 15 '16 at 21:06
  • 3
    I think you wanted to pass `question.id` instead you used `question_id`. If you can just replace this `{% url 'polls:vote' question_id %}` with this `{% url 'polls:vote' question.id %}` it might work I assume. – Aamir Rind Mar 15 '16 at 21:13
  • 1
    If you look at [the tutorial](https://docs.djangoproject.com/en/1.9/intro/tutorial04/#write-a-simple-form), you can see it should be `question.id` as @AamirAdnan says. – Alasdair Mar 15 '16 at 21:17
  • I've read the page many times, looking for the error, but I couldn't find. In the end, it was a simple `.`. As @AamirAdnan said, it's `question.id`. Thanks. –  Mar 15 '16 at 21:29

0 Answers0