I’m currently (very slowly) working through the Django tutorial in the Django Documentation, and I’m up to Part Three. In Part Three, it has you build the detail.html view for the polls you set up in the tutorial.
I’m following along, more or less, but I’m baffled by this bit in the code for detail.html:
<h1>{{ poll.question }}</h1>
<ul>
{% for choice in poll.choice_set.all %}
<li>{{ choice.choice }}</li>
{% endfor %}
</ul>
I understand that first it is presenting the “question” part of the poll (poll.question), and then it’s running through all the choices (choice) in the set of choices for that poll (poll.choice_set.all), but why is it looking for the value “choice” of the choice?
If I change detail.html to have just the following, it still works:
<li>{{ choice }}</li>
Is this an example of something that’s fundamentally Pythonesque or Djangoesque that I should wrap my head around before moving on, or is it just a situation where either option is equally good?
poll.choice_set.all()returns the entire set ofChoices associated with thePoll(via theChoice.pollForeignKey). Thechoiceattribute is theCharFieldwithin theChoicemodel as described in part 1 of the tutorial. Using{{ choice }}works sinceChoice.__unicode__()is defined in part 1 as returning the value of thechoicefield.