栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Python > python框架

Django中template for如何使用?

python框架 更新时间:发布时间: 百科书网 趣学号

之前我们讲过很多次for循环了,python中的循环有不少,不知道有没有听过template for这个循环,这个也算是for循环的这一种延伸。

也许有的小伙伴对template for的用法不是很明确,借着这个机会,今天来讲讲新朋友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 %}


以上就是Django中template for循环使用的使用方法,跟for循环相比较有什么不同呢?小伙伴们恶意找出来再跟小编进行交流。更多Python学习推荐:PyThon学习网教学中心。

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/12699.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号