django template Syntax

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 %}负责



转载于:https://my.oschina.net/012345678/blog/194875

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值