模板语言测试的页面

{#模板语言测试的页面#}

-------------------------变量

{{ name }}

-------------------------取长度

{{ name }} {{ name|length }}

-------------------------文件大小 {{ filesize|filesizeformat }}

{{ age1 }}

-------------------------默认值

{{ bucunzai|default:"这个变量没有传值,使用的是默认值" }}

*****传时间

{{ now }}

{{ now|date:"Y-m-d H:i:s" }}

***取a标签0

{{ a_html }}

{{ a_html|safe }}

**文章摘要

{{ p_str }}


{{ p_str|truncatechars:20 }}

*****列表:

{{ name_list }}

**切片:

{{ name|slice:"0:1" }}

{{ name_list|slice:":" }}

**取列表里的值

**取第一个

{{ name_list.0 }}

*****字典

{{ name_dict }}

{{ name_dict.first_name }}|{{ name_dict.last_name }}

-------------------------使用对象,如果后端没定义__str__方法、则会返回一个对象

{{ person1 }}|{{ person2 }}

-------------------------使用对象的方法

*****只能传不带参数方法

{{ person1.name }}|{{ person1.age }}|{{ person1.dream }}

属性的优先级要大于方法的优先级,字典key的优先级大于内置items()的优先级


{{ p_list }}

{{ p_list.0 }}

-------------------------取列表里的值

{{ p_list.0.name }}

{{ p_list.1.name }}

-------------------------for语句

{% for names in name_lists %}
  • {{ names }}
  • {% empty %}
  • 暂时没有数据
  • {% endfor %}

    ***

    {% for names in name_list %}
  • {{ names }}
  • {% empty %}
  • 暂时没有数据
  • {% endfor %}

    ***

    {% for name in name_list %} {% if forloop.first %} {% endif %} {% if forloop.last %}
  • {{ forloop.counter }}.{{ name }}
  • {% else %}
  • {{ forloop.counter }}.{{ name }}
  • {% endif %} {% endfor %}

    -------------------------if语句

    {% if namelist|length >= 5 %}

    人数大于5

    {% else %}

    人数小于5

    {% endif %}

    -------------------------with用法

    {% with name=namelist|length %} {% if name >= 5 %}

    人数大于5

    {% else %}

    人数小于5

    {% endif %} {% endwith %}

    -------------------------自定义的filter

    {% load myfilter %} {{ name|custom }}|{{ name_dict.first_name|custom }}

    --------------------------自定义的filter传参

    {{ name|add_custom:"传参数的自定义" }}

    -------------------------自定义simple_tag,可以传多个参数

    {% my_sum "Lin" "Yao" "Hong" %}

    -------------------------自定义inclusion_tag 可以返回html代码片段

    {% show_results 10 %}