目前正在进行Django教程(民意调查).
我知道for标签允许你遍历列表或字典,以便做你定义的任何事情.
但是计算标签经过循环的次数的目的是什么?
而且,一个循环的例子不止一次超过同一个列表/字典?
这里有点困惑......
<h1>{{ poll.question }}</h1>
{% if error_message %}
<p><strong>{{ error_message }}</strong></p>
{% endif %}
<form action="{% url 'polls:vote' poll.id %}" method="post">{% csrf_token %}
{% for choice in poll.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>
Run Code Online (Sandbox Code Playgroud)
foorloop.counter在这种情况下的目的是什么?