循环
<ul>
{% for user in user_list %}
<li>{{ user.name }}</li>
{% endfor %}
</ul>
当前循环. | 作用 |
---|---|
forloop.counter | 循环计次 |
forloop.last | 判断是否最后一个循环:bool类型 |
forloop.first | 判断是否第一个循环:bool类型 |
forloop.counter0 | 当前循环的索引值(从0开始) |
forloop.revcounter | 当前循环的倒序索引值(从1开始) |
forloop.revcounter0 | 当前循环的倒序索引值(从0开始) |
forloop.parentloop | 本层循环的外层循环 |
{% for user in user_list %} <!--循环头-->
<tr>
{{ forloop.counter }} <!--循环计次-->
<td>{{ user.id }}</td>
<td>{{ user.names }}</td>
</tr>
{% endfor %} <!--循环尾-->
判断 if elif eles
if语句支持 and 、or、==、>、<、!=、<=、>=、in、not in、is、is not判断。
{% if user_list %}
用户人数:{{ user_list|length }}
{% elif black_list %}
黑名单数:{{ black_list|length }}
{% else %}
没有用户
{% endif %}
判断包含 if..in
{% if id in id_lis %}
执行语句
{% endif %}
for … empty 没数据显示指定信息
<ul>
{% for user in user_list %}
<li>{{ user.name }}</li>
{% empty %}
<!--如果没有数据则显示empty的内容-->
<li>空空如也</li> <!--比如数据库取数据,没信息就提示一句-->
{% endfor %}
</ul>
with 变量名过长,简化名自定义名
{% with total=business.employees.count %}
<!--total就是简化过的变量名-->
{{ total }} employee{{ total|pluralize }}
{% endwith %}