常用的模板标签

  1. if标签。需要{% %}包裹。可以使用==, !=, <, <=, >, >=, in, not in, is, is not等判断运算符
    • 变量名直接写,不用{{}}包裹,包裹的是要输出的
  2. for...in...标签。可以遍历列表,元组,字符串,字典等
    • 添加reversed可以翻转顺序
    • DTL中,执行一个方法不能使用圆括号的形式。遍历字典的时候,如果需要items keys values方法,用dict.items这种方式获得
    • for循环中,DTL提供了一些变量可供使用
      • forloop.counter: 当前循环的下标。以1作为起始值
      • forloop.counter0: 当前循环的下标。以0作为起始值
      • forloop.revcounter: 当前循环的方向下标值。默认1是最后的结束值
      • forloop.revcounter0: 当前循环的方向下标值。默认0是最后你结束值
      • forloop.first: 是否是第一次遍历
      • forloop.last: 是否是最后一次遍历
      • forloop.parentloop: 如果有多次循环嵌套,这个属性代表的是上一级的for循环
    • for...in...没有continue和break!!!!!!!!!!!!!
  3. for...in...empty标签。 如果遍历的对象没有元素,会执行empty中的内容

    • if和for标签

      • 视图代码 ```python def func(request): d = { “number”: 100, “lists”: [

        1. 1,
        2. 2,
        3. "a",

        ], “dicts”: {

        1. "index1": 111,
        2. "index2": 222,
        3. "index3": 333,

        }, “empty_dicts”: {

        },

      } return render(request, “index.html”,context=d) ```

      • 前端代码 ```html <!DOCTYPE html>

        {% if number > 50 %} 大于50 {% endif %}

      {% for list in lists %}

      1. {{ list }}

      {% endfor %}
      {% for list in lists reversed %}

      1. {{ list }}

      {% endfor %}

  1. </h1>
  2. <h2>
  3. {% for dict in dicts %}
  4. {{ dict }}
  5. {% endfor %}
  6. <br>
  7. {% for value in dicts.values %}
  8. 当前的下标是 {{ forloop.counter }}
  9. {{ value }}
  10. <br>
  11. {% endfor %}
  12. <br>
  13. </h2>
  14. <h3>
  15. {% for empty_dict in empty_dicts %}
  16. {{ empty_dict }}
  17. {% empty %}
  18. nothing there
  19. {% endfor %}
  20. </h3>

  1. - 运行 <br />![](https://cdn.nlark.com/yuque/0/2019/png/367873/1559637119367-dd1bfdc9-15d3-4389-96b9-277e3ed84d41.png#align=left&display=inline&height=474&originHeight=474&originWidth=314&size=0&status=done&width=314)
  2. 4. `with`标签。定义变量。有两种使用方式
  3. - 视图代码
  4. ```python
  5. # views.py
  6. d = {
  7. "person": [
  8. "jack",
  9. "lee",
  10. ],
  11. }
  12. def func(request):
  13. return render(request, "index.html", context=d)
  • 前端代码one

    1. <!-- html -->
    2. {% with name=person.0 %}
    3. {{ name }}
    4. {% endwith %}
  • 前端代码two

    1. <!-- html -->
    2. {% with person.0 as name %}
    3. {{ name }}
    4. {% endwith %}
  • 注意=前后不能有空格

  • 这个别名的作用域只在with语句块内
    1. <!-- html -->
    2. {% with person.0 as name %}
    3. {{ name }}
    4. {{ name }}
    5. {% endwith %}
    6. {{ name }} {# 这一句没执行 #}
  1. url标签。 类似reverse()函数。会用到映射时URL指定过的名字进行反转

    1. <!-- html -->
    2. <a href="{% url 'book:list' %}"> 列表 </a>
    3. (# 这里'book:list'是定义在app_name是book的名为list的URL#)
    4. {# url和'book:list'之间需要空格,也就是说空格是分隔符#}
    • 传递参数。和reverse()类似 位置参数和关键字参数不可以同时用。 ```python

      urls.py

      path(“detail//“, views.book_detail,name=”detail”) # 这个url是带参数的

views.py

def book_detail(request, book_id): next = request.GET.get(“page”) if next: return HttpResponse(“book_id is {}, query_string is {}”.format(book_id, next))

  1. ```html
  2. <!-- html -->
  3. {# url反转,使用位置参数 #}
  4. <a href="{% url 'book:detail’ 1 %}"> 页面 </a> {# 位置参数根据URL的变量位置进行排列 #}
  5. {# url反转, 使用关键字参数 #}
  6. <a href="(% url 'book:detail' book_id=1 %)"> 页面 </a>
  • 如果想要传递查询字符串的参数,必须手动在后面添加

    1. <!-- html -->
    2. <a href = "{% url 'book:detal' book_id=1 %}?page=1"> 页面 </a>
  • 如果要传递多个参数,那么通过空格进行分割

    1. <!-- html -->
    2. <a href = {% url 'book:detail' book_id=1 page=2 %}> 页面 </a>
  1. spaceless标签。移除html标签中的空白字符。包括空格、tab键,换行等。

    • 使用

      1. {% spaceless %}
      2. <p>
      3. <a href="index/">INDEX</a>
      4. </p>
      5. {% endspaceless %}
    • 渲染完毕后是以下格式

      1. <p><a href="index/">INDEX</a></p>
    • spaceless只会移除html标签之间的空白字符。而不会移除标签与文本之间的空白字符。

  2. autoescape标签。自动转义,会将哪些特殊字符进行转义,比如会将<转义成&lt;等。DTL默认开启。不容易出现XSS漏洞。

    1. def url_page(request):
    2. d = dict{
    3. "baidu": "<a href = 'http://www.baidu.com'>百度</a>"
    4. }
    1. {% autoescape off %}
    2. {{ baidu }}
    3. {% endautoescape %}
  3. verbatim标签。默认在DTL模板中会去解析哪些特殊字符, 比如{%}%}{{}}等。如果在某个代码片段中不想使用这个解析引擎,比如使用了其他类似的模板时,可以把这个代码片段放在verbatim标签中。

    1. {% verbatim %}
    2. {{ name }}
    3. {% endverbatim %}}