Django Tutorial Issues

374 views
Skip to first unread message

Ivan Cox

unread,
May 4, 2017, 3:13:23 PM5/4/17
to Django users
Hi i am new to Django and in the process of going through the django tutorial at https://www.djangoproject.com/ 
I have however run into problems on Part 4. I have added the following code to the detail.html page 

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

{% if error_message %}<p><strong>{{ error_message }}</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_text }}</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>


But when i click on the links on the polls home page i get the following error

NoReverseMatch at /polls/3/

Reverse for 'vote' with arguments '('',)' not found. 1 pattern(s) tried: ['polls/(?P<question_id>[0-9]+)/vote/$']
Request Method:GET
Request URL:http://127.0.0.1:8000/polls/3/
Django Version:1.11
Exception Type:NoReverseMatch
Exception Value:
Reverse for 'vote' with arguments '('',)' not found. 1 pattern(s) tried: ['polls/(?P<question_id>[0-9]+)/vote/$']
Exception Location:C:\Users\Echalon\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 497
Python Executable:C:\Users\Echalon\AppData\Local\Programs\Python\Python36-32\python.exe
Python Version:3.6.0
Python Path:
['C:\\Web Development\\mysite',
 'C:\\Users\\Echalon\\AppData\\Local\\Programs\\Python\\Python36-32\\python36.zip',
 'C:\\Users\\Echalon\\AppData\\Local\\Programs\\Python\\Python36-32\\DLLs',
 'C:\\Users\\Echalon\\AppData\\Local\\Programs\\Python\\Python36-32\\lib',
 'C:\\Users\\Echalon\\AppData\\Local\\Programs\\Python\\Python36-32',
 'C:\\Users\\Echalon\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages']
Server time:Thu, 4 May 2017 06:31:28 +0000

Error during template rendering

In template C:\Web Development\mysite\polls\templates\polls\detail.html, error at line 5

Reverse for 'vote' with arguments '('',)' not found. 1 pattern(s) tried: ['polls/(?P<question_id>[0-9]+)/vote/$']


1<h1>{{ question.question_text }}</h1>
2
3{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
4
5<form action="{% url 'polls:vote' question.id %}" method="post">
6
7{% csrf_token %}
8{% for choice in question.choice_set.all %}
9 <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
10 <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
11{% endfor %}
12<input type="submit" value="Vote" />
13</form>
14

Any help would be greatly appreciated.
















































René Fleschenberg

unread,
May 4, 2017, 3:50:45 PM5/4/17
to django...@googlegroups.com
Hi Ivan,

It is very likely that your problem is caused by one of these:

- `question` is not assigned to the template context
- `question` is assigned to the template context, but it has a wrong
value (e.g. `None`)
- the question has no `id` (it is not saved to the database yet).


This then results in this line:

`{% url 'polls:vote' question.id %}`

being something like

{% url 'polls:vote' "" %}

which does not match your URL pattern, because the pattern wants an
integer for the id.

I hope this helps!

René


On 04.05.2017 08:32, Ivan Cox wrote:
> Hi i am new to Django and in the process of going through the django
> tutorial at https://www.djangoproject.com/
> I have however run into problems on Part 4. I have added the following
> code to the detail.html page
>
> |
> <h1>{{ question.question_text }}</h1>
>
> {% if error_message %}<p><strong>{{ error_message }}</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_text
> }}</label><br />
> {% endfor %}
> <input type="submit" value="Vote" />
> </form>
>
> |
>
> But when i click on the links on the polls home page i get the following
> error
>
>
> NoReverseMatch at /polls/3/


--
René Fleschenberg

Am Stadtgarten 28, 45276 Essen, Germany
Phone: +49 1577 170 7363
E-Mail: re...@fleschenberg.net
Reply all
Reply to author
Forward
0 new messages