Django templates 模板的语法

  1. 变量
    {{ new_name }} ——》 变量 新华出版社 字典或列表用 . 取值
  2. for循环for 标签带有一个可选的{% empty %} 从句,以便在给出的组是空的或者没有被找到时,可以有所操作。
    {% for person in person_list %}

    {{ person.name }}

    # 循环取值

    {% empty %}
        <p>sorry,no person here</p>   #  person_list 为空时 显示
    {% endfor %} 
  3. if 标签 :{% if %}会对一个变量求值,如果它的值是“True”(存在、不为空、且不是boolean类型的false值),对应的内容块会输出。
    {% if i > 300 %}

    大于{{ i }}


    {% elif i == 200 %}

    等于{{ i }}


    {% else %}

    小于{{ i }}


    {% endif %}
  4. .with:使用一个简单地名字缓存一个复杂的变量,当你需要使用一个“昂贵的”方法(比如访问数据库)很多次的时候是非常有用的
    {% with total=business.employees.count %}
    {{ total }} employee{{ total|pluralize }}
    {% endwith %}

    {{ person_list.2.name }}


    {% with name=person_list.2.name %}

    {{ name }}


    {% endwith %}

猜你喜欢

转载自www.cnblogs.com/zhang-zi-yi/p/10158903.html