[英]How do I add an incremental ID to my td?
My html file generates a table: 我的html文件生成一个表:
{% for resp in results %}
<tr style="counter-increment: count">
<td>{{ resp }}</td>
<td>{{ resp.Question_id.Statement }}</td>
</tr>
{% endfor %}
How do I assign an id to the first td every time a new row is generated? 每次生成新行时,如何为第一个td分配ID?
use the forloop.counter
template var : 使用forloop.counter
模板var :
{% for resp in results %}
<tr style="counter-increment: {{ forloop.counter }}">
<td>{{ resp }}</td>
<td>{{ resp.Question_id.Statement }}</td>
</tr>
{% endfor %}
Use: 采用:
forloop.counter The current iteration of the loop (1-indexed)
More info at: https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#for 有关更多信息, 请访问: 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 %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.