{% if %}标签
可以没有else标签:
{% if today_is_weekend %}
<p>Welcome to the weekend!</p>
{% endif %}
可以有else标签:
{% if today_is_weekend %}
<p>Welcome to the weekend!</p>
{% else %}
<p>Get back to work.</p>
{% endif %}
几个知识点
python中的“flase值”
- 空列表[]
- 空元祖()
- 空字典{}
- 空字符串”
- 零值0
- 特殊对象None
- 对象false
- 自定义的对象的布尔值属性(python高级用法)
{% if %}标签接受and,or和not逻辑运算符,同一个标签中可以接受多个同种的逻辑运算符,但是不接受同一个标签中同时使用不同的逻辑运算符,即使使用圆括号来组合比较操作也不支持,此时建议使用嵌套{% if %}标签。
- 没有{% elif %}标签,请使用嵌套{% if %}标签。
- 一定要使用{% endif %}标签来关闭{% if %}标签。
{% for %}标签
用于遍历列表list
正常使用方法:
<ul>
{% for athlete in athlete_list %}
<li>{{ athlete.name }}</li>
{% endfor %}
</ul>
给{% for %}标签加一个reversed,使列表反向迭代:
<ul>
{% for athlete in athlete_list reversed %}
<li>{{ athlete.name }}</li>
{% endfor %}
</ul>
建议在执行循环之前检测列表的大小:
{% if athlete_list %}
{% for athlete in athlete_list reversed %}
<p>{{ athlete.name }}</p>
{% endfor %}
{% else %}
<p>There are no athletes. Only computer programmers.</p>
{% endif %}
由于这种做法很常见,所以{% for %}标签支持一个可选的{% empty %}分句,来定义列表为空时的输出内容:(与上面的例子等价)
{% if athlete_list %}
{% for athlete in athlete_list reversed %}
<p>{{ athlete.name }}</p>
{% endfor %}
{% empty %}
<p>There are no athletes. Only computer programmers.</p>
{% endif %}
**注意:**django不支持break和continue语句
forloop模板变量
在每一个{% for %}标签里有个称为“forloop”的模板变量,这个变量有一些提示循环进度信息的属性
forloop.counter:表示当前循环的执行次数的整数计数器,从1开始计数。
{% for item in todo_list %} <p>{{ forloop.counter }}: {{ item }}</p> {% endfor %}
forloop.counter0:从0开始计数。
- forloop.revcounter:表示循环中剩余项的整型变量,最后一次循环执行中,这个变量被置为1。
- forloop.revcounter0:以0作为结束索引。
forloop.first:是一个布尔值,在该迭代第一次执行时为True,之后都是false。
{% for object in objects %} {% if forloop.first %}<li class="first">{% else %}<li>{% endif %} {{ object }} </li> {% endfor %}
forloop.last:是一个布尔值,在该迭代最后一次执行时为True,其余次都是false。
常见的使用方法:在一系列的链接之间放置管道符|
{% for link in links %}{{ link }}{% if not forloop.last %} | {% endif %}{% endfor %}
上述模板可能产生如下的结果:
Link1 | Link2 | Link3 | Link4
另一个常见的用途:为列表的每个单词加上逗号,
Favorite places: {% for place in places %}{{place}}{% if not forloop.last %},{% endif %}{% endfor %}
forloop.patentloop:是一个指向当前循环的上一级循环的forloop对象的引用。
{% for country in countries %} <table> {% for city in country.city_list %} <tr> <td>Country #{{ forloop.parentloop.counter }}</td> <td>City #{{ forloop.counter }}</td> <td>{{ city }}</td> </tr> {% endfor %} </table> {% endfor %}
{% ifequal %}标签
{% ifequal %}标签用于比较两个模板变量uesr和current_user,用法类似于{% if %}标签
没有{% else %}标签的:
{% ifequal user current_user %}
<h1>Welcome!</h1>
{% endifequal %}
有{% else %}标签:
{% ifequal section 'sitenews' %}
<h1>Site News</h1>
{% else %}
<h1>No News Here</h1>
{% endifequal %}
注意这里的’sitenews’没有问题,也可以是”sitenews”。因为参数支持硬编码的字符串。另外{% ifequal %}标签还支持字符串,整数和小数。但是不支持Python中的字典类型、列表类型、布尔类型。
合法:
{% ifequal variable 1 %}
{% ifequal variable 1.23 %}
{% ifequal variable 'foo' %}
{% ifequal variable "foo" %}
不合法:
{% ifequal variable True %}
{% ifequal variable [1, 2, 3] %}
{% ifequal variable {'key': 'value'} %}
注释
单行注释:
{# This is a comment #}
多行注释:
{% comment %}
This is a comment
This is a comment
This is a comment
This is a comment
This is a comment
{% endcomment %}