我的html文件生成一个表:
{% for resp in results %}
<tr style="counter-increment: count">
<td>{{ resp }}</td>
<td>{{ resp.Question_id.Statement }}</td>
</tr>
{% endfor %}
每次生成新行时,如何为第一个td分配ID?
答案 0 :(得分:3)
使用forloop.counter
template var:
{% for resp in results %}
<tr style="counter-increment: {{ forloop.counter }}">
<td>{{ resp }}</td>
<td>{{ resp.Question_id.Statement }}</td>
</tr>
{% endfor %}
答案 1 :(得分:2)
使用:
forloop.counter The current iteration of the loop (1-indexed)
更多信息,请访问: https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#for
{% for item in item_list %}
{{ forloop.counter }} {# starting index 1 #}
{{ forloop.counter0 }} {# starting index 0 #}
{# do your stuff #}
{% endfor %}