【问题标题】:How do I add an incremental ID to my td?如何向我的 td 添加增量 ID?
【发布时间】:2019-01-03 10:26:47
【问题描述】:

我的html文件生成表格:

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

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

【问题讨论】:

标签: python html django count


【解决方案1】:

使用forloop.countertemplate var:

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

【讨论】:

    【解决方案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 %}
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 2016-02-08
    • 2016-12-08
    相关资源
    最近更新 更多