如何在我的td中添加增量ID?

时间:2018-07-26 12:12:57

标签: python html django count

我的html文件生成一个表:

{% for resp in results %}
    <tr style="counter-increment: count">
         <td>{{ resp }}</td>
         <td>{{ resp.Question_id.Statement }}</td>
    </tr>
{% endfor %}

每次生成新行时,如何为第一个td分配ID?

2 个答案:

答案 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 %}