首页 > 其他 > 详细

Django template for 循环用法

时间:2016-10-31 06:43:35      阅读:374      评论:0      收藏:0      [点我收藏+]

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

{% 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 %}

 

Django template for 循环用法

原文:http://www.cnblogs.com/haoshine/p/6014474.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!