Tutorial part 4 help: the raidio buttons to make a choice on the polls question won't show.

424 views
Skip to first unread message

Othniel Ayinzat

unread,
Oct 20, 2016, 11:52:59 AM10/20/16
to Django users
Hi guys,
 I have been following the django tutorials up to part 4 on the polls application. My details template only displays the vote submit button but the radio buttons for making choices won't show. I have been battling with this for some hours now and i need help or some pointers. Thanks. Below are the code snippets for the polls app.

polls/templates/polls/detail.html
-----------------------------------------------
<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>

polls/templates/polls/index.html
-----------------------------------------------

{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}

Othniel Ayinzat

unread,
Oct 21, 2016, 6:32:34 AM10/21/16
to Django users
I think the problem is from around:

 {% 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 %}

This is because i tested outside the loop and i got the radio button,  but the whole idea was to loop through the questions and assign a button for the choices.

ludovic coues

unread,
Oct 22, 2016, 3:44:49 PM10/22/16
to django...@googlegroups.com
I would try to add that right before the loop:

{{ question|pprint }}
{{ question.choice_set.all|pprint }}

The result should be something like that

<class 'app.Question'>
<choices list([choice a, choice b])>

the pprint filter is for debugging, you should remove once your issue
is fixed. It will make obvious some error like question.choice_set.all
not returning the kind of data you expect.
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/529b45cc-c1ec-46d8-90eb-26c30aad2989%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

Brady Lee

unread,
Feb 7, 2017, 4:27:13 AM2/7/17
to Django users
Same issue here, no radio buttons show up.

ubuntu 16.04.1 LTS
python 3.5.2
django 1.10.5

I've tried  {{ question|pprint }} and {{ question.choice_set.all|pprint }} for debugging, got:

<Question: What's up?> <QuerySet []>

Now trying to figure out where the problem is. 

ludovic coues

unread,
Feb 7, 2017, 5:17:58 AM2/7/17
to django...@googlegroups.com
Have you added some choices to the "What's up?" question ?

What you see is that question.choice_set.all is empty.
The following snippet will give you some output:

{% 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 />
{% empty %}
<p>Sorry, no available choices.</p>
{% endfor %}
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/8a5fe550-62bf-42e5-8dfa-0b3fc8d5d4cf%40googlegroups.com.

Brady Lee

unread,
Feb 10, 2017, 12:42:50 AM2/10/17
to Django users
The "polls_choice" table in database file db.sqlite3 has no records, so no radio buttons show up.
I manually added 2 records to the "polls_choice" table, then I have the radio buttons showed up.
Reply all
Reply to author
Forward
0 new messages