django 基本模板标签

标签

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

if/else

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

复制代码
{% 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来测试多变量,参考下面的例子:

复制代码
{% 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同时出现在一个标签语句中,因为这样会引起歧义。例如:

{% if athlete_list and coach_list or cheerleader_list %}

括号在这里不支持的,如果你有需要,可以考虑将逻辑放在模板的外层,并将指定的模板变量作为结果传出来。或者嵌套{%if%}标签:

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

多次使用同一操作符是可以的,但同时使用多个不同的操作符是不可以的。下面的语句是有效的:

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

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

复制代码
{% 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 }}

 

posted @ 2015-11-16 10:34  dengzk  阅读(2457)  评论(0编辑  收藏  举报