通过使用 ``{% for obj in list reversed %}`` 你也可以实现反序循环.
在循环过程中 for 循环会设置以下的一系列变量:
========================== ================================================
Variable Description
========================== ================================================
``forloop.counter`` 当前循环次数 (1-indexed)
``forloop.counter0`` 当前循环次数 (0-indexed)
``forloop.revcounter`` 倒序循环时当前循环次数(1-indexed)
``forloop.revcounter0`` 倒序循环时当前循环次数(0-indexed)
``forloop.first`` 如果当前循环是循环过程的第一次则为True
``forloop.last`` 如果当前循环是循环过程的最后一次则为True
``forloop.parentloop`` 对嵌套循环, 当前循环之上的循环
========================== ================================================
if
~~
``{% if %}`` 标签对一个变量求值, 若这个变量为 "true" , 就输出 ``if`` 内容块::
{% if athlete_list %}
Number of athletes: {{ athlete_list|length }}
{% else %}
No athletes.
{% endif %}
在上例中, 如果 ``athlete_list`` 非空, 运动员人数就会通过 ``{{ athlete_list|length }}`` 变量显示出来.
就象你刚刚看到的, ``if`` 标签可以带一个 ``{% else %}`` 子句,用来当 ``if`` 测试失败后输出相应的内容块.
``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 (OK, so
writing English translations of boolean logic sounds
stupid; it's not our fault).
{% endif %}
{% if athlete_list and not coach_list %}
There are some athletes and absolutely no coaches.
{% endif %}
为避免造成歧义, ``if``标签不允许在一个 tag 中同时有 ``and`` 和 ``or`` 逻辑; 举个例子,下面这个语句不能工作::
{% if athlete_list and coach_list or other_list %}
如果确实需要组合条件,可以使用嵌套的 ``if`` 实现相同的功能::
{% if athlete_list %}
{% if coach_list or other_list %}
We have athletes and , either coaches or others.
{% endif %}
{% endif %}
ifchanged
~~~~~~~~~
检查一个变量自上次循环之后是否发生了改变.(我的理解:主要用于过滤掉重复的值)
'ifchanged' block标签用于循环中. 它根据自身上次的状态检查自己值, 只有值发生变化时才显示这个值::
Archive for {{ year }}
{% for day in days %}
{% ifchanged %}
{{ day|date:"F" }}
{% endifchanged %}
{{ day|date:"j" }}
{% endfor %}
ifequal
~~~~~~~
若两个参数相等,输出一个内容块.
例子::
{% ifequal user.id comment.user_id %}
...
{% endifequal %}
如同 ``{% if %}`` tag, 它也支持一个可选的 ``{% else %}`` 子句.
参数可以是变量,也可以是字符串字面值, 也就是说下面这样也是合法的::
{% ifequal user.username "adrian" %}
...
{% endifequal %}
ifnotequal
~~~~~~~~~~
类似 ``ifequal``, 只是它用来测试两个参数是否不等.
include
~~~~~~~
载入一个模板并根据当前上下文渲染它.用于在一个模板中包含其它模板.
模板名字可以是一个变量,也可以是一个字符串(带引号的字符串,无所谓单引号还是双引号).
下面这个例子包含了 ``"foo/bar.html"`` 模板的内容::
{% include "foo/bar.html" %}
下面这个例子包含了另一个模板(该模板的名字为变量 ``template_name`` 的值)的内容::
{% include template_name %}
被包含的模板使用包含它的模板的上下文(也就是环境)进行渲染(求值),下面这个例子输出 ``"Hello, John"``:
* Context: variable ``person`` is set to ``"john"``.
* Template::
{% include "name_snippet.html" %}
* The ``name_snippet.html`` template::
Hello, {{ person }}
参阅: ``{% ssi %}``.
load
~~~~
装入一个自定义模板标签集.
参阅 `自定义标签及过滤器库`_ 以了解更多信息.
now
~~~
显示当前日期, 根据给定的字符串决定输出格式.
使用和 PHP 的 ``date()`` 函数一样的格式码 (http://php.net/date) 并做了一些扩展
可用的格式字符串:
================ ======================================== =====================
格式字符 描述 输出示例
================ ======================================== =====================
a ``'a.m.'`` 或 ``'p.m.'`` (注意,它与PHP ``'a.m.'``
的输出略有不同.它包括了句点(django扩展).
A ``'AM'`` 或 ``'PM'``. ``'AM'``
B 未实现.
d 每月第几天, 带前导零 ``'01'`` to ``'31'``
D 每周第几天,3字母的字符串. ``'Fri'``
f 时间, 12-小时制的小时和分钟数, ``'1'``, ``'1:30'``
如果分钟数为零,则不显示.(django 扩展).
F 月份, 长文本格式. ``'January'``
g 小时, 12-小时制,没有前导零 ``'1'`` to ``'12'``
G 小时, 24-小时制,没有前导零 ``'0'`` to ``'23'``
h 小时, 12-小时制,有前导零 ``'01'`` to ``'12'``
H 小时, 24-小时制,有前导零 ``'00'`` to ``'23'``
i 分钟. ``'00'`` to ``'59'``
I 未实现
j 每月第几天, 无前导零 ``'1'`` to ``'31'``
l 每周第几天,长文本格式. ``'Friday'``
L 是否闰年. ``True`` or ``False``
m 数字表示的月份,有前导零. ``'01'`` to ``'12'``
M 月份,3字母短文本格式. ``'Jan'``
n 数字表示的月份,无前导零 ``'1'`` to ``'12'``
N 出版风格的月份缩写(django 扩展) ``'Jan.'``, ``'Feb.'``, ``'March'``, ``'May'``
O 与格林威治的时间差(以小时计) ``'+0200'``
P 12小时制的小时分钟及'a.m.'/'p.m.' ``'1 a.m.'``, ``'1:30 p.m.'``, ``'midnight'``, ``'noon'``, ``'12:30 p.m.'``
分钟数若为零则不显示. 用字符串表示特殊
的时间点, 如 'midnight' 和 'noon'
(django扩展)
r RFC 822 格式的日期 . ``'Thu, 21 Dec 2000 16:01:07 +0200'``
s 秒数, 带有前导零的数字表示 ``'00'`` to ``'59'``
S 英语序数后缀,用于一个月的第几天,2个字符 ``'st'``, ``'nd'``, ``'rd'`` or ``'th'``
t 给定月共有多少天. ``28`` to ``31``
T 本机时区. ``'EST'``, ``'MDT'``
U 未实现
w 一周中的第几天,没有前导零的数字 ``'0'`` (Sunday) to ``'6'`` (Saturday)
W ISO-8601 一年的第多少星期数, 一周从 ``1``, ``23``
星期一开始
y Year, 2 位数字表示 ``'99'``
Y Year, 4 位数字表示 ``'1999'``
z 一年中的第几天 . ``0`` to ``365``
Z 以秒计的时区偏移量. 这个偏移量对UTC西部 ``-43200`` to ``43200``
时区总是负数,而对UTC东部时区则总是正数
================ ======================================== =====================
例子::
It is {% now "jS F Y H:i" %}
注意你可以使用反斜线转义一个格式字符串中的敏感字符.(如果你想使用其原始值的话).在下面这个例子里, "f" 被用反斜线转义, 因为
"f" 本身是一个用于显示时间的格式字符. "o" 不需要被转义,因为它不是一个格式字符.::
It is the {% now "jS o\f F" %}
(显示 "It is the 4th of September" %}
regroup
~~~~~~~
Regroup a list of alike objects by a common attribute.
要搞懂这个复杂的标签, 最好还是用一个例子来说明(一幅图胜过千句话): 有一个 ``people`` 对象,它是一个 ``Person`` 对象(拥有 ``first_name``, ``last_name`` 及 ``gender`` 属性)的列表. 你想显示一个象下面这样的列表:
* Male:
* George Bush
* Bill Clinton
* Female:
* Margaret Thatcher
* Condoleezza Rice
* Unknown:
* Pat Smith
下面的模板代码片断可以完成这个看上去复杂的任务::
{% regroup people by gender as grouped %}