django 基本模板标签

标签oop

下面的部分概述了常见的Django标签。测试

if/else编码

{%if%} 标签 对一个变量值进行测试,若是结果为true,系统将会显示在{%if%} 和 {%endif%}之间的一切,看个例子:spa

复制代码
{% if today_is_weekend %} <p>Welcome to the weekend!</p> {% endif %} An {% else %} tag is optional: {% if today_is_weekend %} <p>Welcome to the weekend!</p> {% else %} <p>Get back to work.</p> {% endif %}
复制代码

{%if%} 标签接受 and,or,not来测试多变量,参考下面的例子:code

复制代码
{% if athlete_list and coach_list %} Both athletes and coaches are available. {% endif %} {% if not athlete_list %} There are no athletes. {% endif %} {% if athlete_list or coach_list %} There are some athletes or some coaches. {% endif %} {% if not athlete_list or coach_list %} There are no athletes or there are some coaches. {% endif %} {% if athlete_list and not coach_list %} There are some athletes and absolutely no coaches. {% endif %}
复制代码

{%if%}标签不接受and和or同时出如今一个标签语句中,由于这样会引发歧义。例如:blog

{% if athlete_list and coach_list or cheerleader_list %}

括号在这里不支持的,若是你有须要,能够考虑将逻辑放在模板的外层,并将指定的模板变量做为结果传出来。或者嵌套{%if%}标签:it

{% if athlete_list %} {% if coach_list or cheerleader_list %} We have athletes, and either coaches or cheerleaders! {% endif %} {% endif %}

屡次使用同一操做符是能够的,但同时使用多个不一样的操做符是不能够的。下面的语句是有效的:io

{% if athlete_list or coach_list or parent_list or teacher_list %}

没有{%elif%}标签,用嵌套的{%if%}标签来完成一样的是事情:ast

复制代码
{% if athlete_list %} <p>Here are the athletes: {{ athlete_list }}.</p> {% else %} <p>No athletes are available.</p> {% if coach_list %} <p>Here are the coaches: {{ coach_list }}.</p> {% endif %} {% endif %}
复制代码

确保每一个{%if%}对应一个{%endif%},不然Django将会抛出一个TemplateSyntaxError 的异常。模板

for

{%for%}容许你对一个序列中的每一项进行循环,格式是 for X in Y,Y是用来遍历的集合,X是变量名。每次循环,模板系统都会将{%for%}跟{%endfor%}中内容展示出来。

例如:

<ul>
    {% for athlete in athlete_list %} <li>{{ athlete.name }}</li> {% endfor %} </ul>

还能够对集合进行反转遍历:

{% for athlete in athlete_list reversed %} ... {% endfor %}

还能够进行嵌套:

复制代码
{% for athlete in athlete_list %} <h1>{{ athlete.name }}</h1> <ul> {% for sport in athlete.sports_played %} <li>{{ sport }}</li> {% endfor %} </ul> {% endfor %}
复制代码

 经常使用的一个模式是在循环遍历以前检查List的大小,若是List为空则输出一些特殊的文本

复制代码
{% if athlete_list %} {% for athlete in athlete_list %} <p>{{ athlete.name }}</p> {% endfor %} {% else %} <p>There are no athletes. Only computer programmers.</p> {% endif %}
复制代码

由于这个方法太过经常使用,for标签支持一个可选的{%Empty%}的选项让你定义输出自定义的文本。例如:

{% for athlete in athlete_list %} <p>{{ athlete.name }}</p> {% empty %} <p>There are no athletes. Only computer programmers.</p> {% endfor %}

注意,for标签不支持break,countinue。

在{%for%}标签中,你能够访问forloop变量,该变量有几个经常使用的属性:

1.forloop.counter:用于记录循环了多少次

2.forloop.counter0:相似forloop.counter0,只不过是从0开始的。

3.forloop.revcounter:用于记录还未被遍历的项目数

4.forloop.revcounter0:相似revcounter,不过计数是从0开始

5.forloop.first:布尔值,用来标识是否为第一个项目

6.forloop.last:布尔值,用来表示是否为最后一个项目

ifequal/ifnotequal

{%ifequal%} 标签比较两个值,若是他们相等的话,显示在{%ifequal%} 和{%endifequal%}之间的全部代码。

{% ifequal user currentuser %}
<h1>Welcome!</h1> {% endifequal %}

参数能够是硬编码,因此下面的例子都是有效的:

复制代码
{% ifequal section 'sitenews' %} <h1>Site News</h1> {% endifequal %}

{% ifequal section "community" %}
<h1>Community</h1>
{% endifequal %}

复制代码

跟{%if%}相似,the {%ifequal%}标签也支持选项{%else%}

{% ifequal section 'sitenews' %} <h1>Site News</h1> {% else %} <h1>No News Here</h1> {% endifequal %}

只有模板变量、字符、整型,和浮点型能够做为对比参数,下面是一些有效的例子:

{% ifequal variable 1 %}
{% ifequal variable 1.23 %} {% ifequal variable 'foo' %} {% ifequal variable "foo" %}

其余类型如List、字典、布尔类型的都不能够做为{%ifequal%}的参数。

{% ifequal variable True %}
{% ifequal variable [1, 2, 3] %} {% ifequal variable {'key': 'value'} %}

这些是无效的参数。若是你要测试某些东西是否为True或False,用{%ifequal%}

注释

使用{##},多行注释则使用{%comment%}和{%endcoomment%}

 

过滤器

模板过滤是在显示他们以前变动变量值的最简单的方法。如:{{name|lower}},这会先将name的值变为小写,而后再显示出来。

过滤器是能够多个接连使用的:

{{ my_list|first|upper }}
相关文章
相关标签/搜索