variables
{{ variable }}
{{ variable.sttributes}}实际尝试以下几种
Dictionary lookup 字典
{% for k, v in defaultdict.iteritems %}
Do something with k and v here...
{% endfor %}
Attribute lookup 属性
Method call 方法
List-index lookup 列表
重点:
如果你使用一个不存在变量 ,模板系统使用 TEMPLATE_STRING_IF_INVALID值,如果你在settings定义了,否则就是空‘ ’
Filters
ex {{ name| lower}}
链式写法
{{ text|escape|linebreaks }}
参数
{{ bio|truncatewords:30 }}
这种也可以
{{ list|join:", " }}
django大约提供了30个filters
重点;
{{ value|default:"nothing" }}
传入变量,但为none或空 使用默认值,和TEMPLATE_STRING_IF_INVALID有区别
Tags
{% for %} {% endfor%}
{%if%}{%endif%}
{%if%} {%else%}{%endif%}
{%if%} {%elif%} {%else%}{%endif%}
exends 继承
通常使用三级继承
base.html base_two.html base_specific.html
注意{% block.super%}
指定哪个block关闭,在有很多个block时很好用
{% block content %}...{% endblock content %}
Automatic HTML escaping 略。自看
说两点:
为了安全需要转义潜在的一些不安全符号
可以控制转义还是不转义
Accessing method calls
访问对象的属性和方法
{% for comment in task.comment_set.all %}
{{ comment }}
{% endfor %}
注意这个,自己在models里写的方法一样可以调用
# In model
class Task(models.Model):
def foo(self):
return "bar"
# In template{{ task.foo }}
Custom tag and filter libraries 自定义标签和过滤器
使用{% load xxx %}载入你自定的标签和过滤器.py名字
可以有多个{% load %}
Custom libraries and template inheritance
加载自定义标记或滤波器库时,标签/过滤器只提供当前的模板使用
例如,如果一个模板foo.html { %load comment % },a child template ,{ %extends“foo.html“% }将无法使用父模板标签和过滤器。子模板只对他自己加载的 {% load comments %}负责