for 使用形式: {% for variable in list/dict %} (使用variable) {% endfor%} 意义:循环list中的每个值,进行相应的输出 注意: (a)也可以反向遍历{% for variable in list/dict reversed %} (b)也可以{% for x, y in points %} points中的每个元素为 (x,y) (c)也可以{% for key,value in data.items %} data是一个dictionary for loop中定义的一些内建变量 forloop.counter 当前的迭代器数目(从1开始) forloop.counter0 当前的迭代器数目(从0开始) forloop.revcounter 当前的反向迭代器数目(从1开始) forloop.revcounter0 当前的反向迭代器数目(从0开始) forloop.first 值为True,如果是第一次通过迭代器 forloop.last 值为True,如果是最后一次通过迭代器 forloop.parentloop 对于嵌套循环,这是当前循环的上一层循环
for ... empty 使用形式如下: {% for varibale in list %} (内容1) {% empty %} (内容2) {% endfor %} 意义:当list是空的时候,能够执行内容2,其形式等同于,先if判断list是否存在,然后在根据情况做什么操作。
if 使用形式如下 : {% if variable %} (内容1) {% else %} (内容2) {% endif %} 注意:variable中可以使用and or 或者not,但是有一条必须记住,就是不允许and 和 or一起使用
ifequal 使用形式: {% ifequal variable1 variable2 %} ... {% endifequal %} 意义:判断两个变量是否相等。 ifnotequal 使用与(12)相同 include 使用形式:{% include "foo/bar.html" %}或者{% include template_name %} 意义:将另外一个模板文件中的内容添加到该文件中。注意区别extend是继承。 now 使用形式:{% now "jS F Y H:i "%},注意存在需要转义的情况例如{% now "jS o\f F" %},因为f是格式化字符串 具体的格式化字符串如下所示
a 'a.m.' or 'p.m.' (Note that this is slightly different than PHP's output, because this includes periods to match Associated Press style.) 'a.m.' A 'AM' or 'PM'. 'AM' b Month, textual, 3 letters, lowercase. 'jan' B Not implemented. d Day of the month, 2 digits with leading zeros. '01' to '31' D Day of the week, textual, 3 letters. 'Fri' f Time, in 12-hour hours and minutes, with minutes left off if they're zero. Proprietary extension. '1', '1:30' F Month, textual, long. 'January' g Hour, 12-hour format without leading zeros. '1' to '12' G Hour, 24-hour format without leading zeros. '0' to '23' h Hour, 12-hour format. '01' to '12' H Hour, 24-hour format. '00' to '23' i Minutes. '00' to '59' I Not implemented. j Day of the month without leading zeros. '1' to '31' l Day of the week, textual, long. 'Friday' L Boolean for whether it's a leap year. True or False m Month, 2 digits with leading zeros. '01' to '12' M Month, textual, 3 letters. 'Jan' n Month without leading zeros. '1' to '12' N Month abbreviation in Associated Press style. Proprietary extension. 'Jan.', 'Feb.', 'March', 'May' O Difference to Greenwich time in hours. '+0200' P Time, in 12-hour hours, minutes and 'a.m.'/'p.m.', with minutes left off if they're zero and the special-case strings 'midnight' and 'noon' if appropriate. Proprietary extension. '1 a.m.', '1:30 p.m.', 'midnight', 'noon', '12:30 p.m.' r RFC 2822 formatted date. 'Thu, 21 Dec 2000 16:01:07 +0200' s Seconds, 2 digits with leading zeros. '00' to '59' S English ordinal suffix for day of the month, 2 characters. 'st', 'nd', 'rd' or 'th' t Number of days in the given month. 28 to 31 T Time zone of this machine. 'EST', 'MDT' U Not implemented. w Day of the week, digits without leading zeros. '0' (Sunday) to '6' (Saturday) W ISO-8601 week number of year, with weeks starting on Monday. 1, 53 y Year, 2 digits. '99' Y Year, 4 digits. '1999' z Day of the year. 0 to 365 Z Time zone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.