Reputation: 51
I'm following a tutorial on Django's official site (https://docs.djangoproject.com/en/2.0/intro/tutorial04/), everything went fine until I came to the part where they had me create an html form using Django templates, here's the template and form (namely, detail.html):
<h1>{{ question.question_text }}</h1>
<ul>
<!--{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }}</li>
{% endfor %}-->
<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }}</li>
<label for="choice{{ forloop.counter }}">{{ choice.choice_text}}</label><br />
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
{% endfor %}
<input type="submit" value="Vote" />
</form>
</ul>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
Here's the view:
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/detail.html', {'question': question})
The part where I start to get problems is at the radio button (in template), it doesn't display on the screen at all, here's the output:
I tried rearanging the code a little in hope that there might be a syntax error, then I tried finding the solution by watching tutorials on youtube but no success, come somebody help me?
Upvotes: 0
Views: 1035
Reputation: 174
I had what is probably the same issue.
I had skipped this part: https://docs.djangoproject.com/en/2.2/intro/tutorial02/#playing-with-the-api
This is where you use the API/Python shell to add the voting choices manually via the command line.
Upvotes: 2
Reputation: 1
Theory: I just explain the following row, before I get to the mistake.
{% for choice in question.choice_set.all %}
question
: selected ID of the question you chose in the URL http://localhost:8000/polls/1/ --> 1
.choice_set
: is a query on table polls_choice
and because of the fact, that in the tutorial a foreign key on Choice was created, the related entry for ID = 1 can be found.
.all
: means that each entry with the related foreign key will be used.
Resolving: I think, you skipped the part of tutorial 02 https://docs.djangoproject.com/en/2.2/intro/tutorial02/ , where you have to create an table entry for polls_choice. Create_table_entry_polls_choice . That is why the for loop is not running, because there are no entries. Just repeat that, or create the entries manual in your MYSQL database or whatever you use.
Upvotes: 0