• Django template for 循环用法


    当列表为空或者非空时执行不同操作:

    {% for item in list %}
        ...
    {% empty %}
        ...
    {% endfor %}

    使用forloop.counter访问循环的次数,下面这段代码依次输出循环的次数,从1开始计数:

    {% for item in list %}
        ...
        {{ forloop.counter }}
        ...
    {% endfor %}

    从0开始计数:

    {% for item in list %}
        ...
        {{ forloop.counter0 }}
        ...
    {% endfor %}

    判断是否是第一次循环:

    {% for item in list %}
        ...
        {% if forloop.first %}
            This is the first round. 
        {% endif %}
        ...
    {% endfor %}

    判断是否是最后一次循环:

    {% for item in list %}
        ...
        {% if forloop.last %}
            This is the last round.
        {% endif %}
        ...
    {% endfor %}

    逆向循环:

    {% for item in list reversed %}
        {{ item }}
    {% endfor %}
  • 相关阅读:
    多层交换概述
    多层交换MLS笔记2
    多层交换MLS笔记1
    RSTP Proposal-Agreement
    RSTP Note
    保护STP
    优化STP
    Cisco STP Note
    25、C++的顶层const和底层const
    43、如何用代码判断大小端存储
  • 原文地址:https://www.cnblogs.com/haoshine/p/6014474.html
Copyright © 2020-2023  润新知