Django2.0-templates(3)-模版标签

常用的模板标签

  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标签

      def func(request):
          d = {
              "number": 100,
              "lists": [
                  1,
                  2,
                  "a",
              ],
              "dicts": {
                  "index1": 111,
                  "index2": 222,
                  "index3": 333,
              },
              "empty_dicts": {
      
              },
      
          }
          return render(request, "index.html",context=d)
      
      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <title>Title</title>
      </head>
      <body>
          <h1>
              {% if number > 50 %}
                  大于50
              {% endif %}
          </h1>
      
          <h1>
              {% for list in lists %}
                  {{ list }}
              {% endfor %}
              <br>
              {% for list in lists reversed %}
                  {{ list }}
              {% endfor %}
      
      
          </h1>
      
          <h2>
              {% for dict in dicts %}
                  {{ dict }}
              {% endfor %}
      
              <br>
              {% for value in dicts.values %}
                  当前的下标是 {{ forloop.counter }}
                  {{ value }}
                  <br>
              {% endfor %}
              <br>
          </h2>
          <h3>
                 {% for empty_dict in empty_dicts %}
                     {{ empty_dict }}
                 {% empty %}
                     nothing there
                 {% endfor %}
      
          </h3>
      </body>
      </html>
      

      运行

      run1

  4. with标签。定义变量。有两种使用方式

    # views.py
    d = {
        "person": [
          	"jack",
            "lee",
        ],
    }
    
    def func(request):
    	return render(request, "index.html", context=d)
    
    • one

      <!-- html -->
      {% with name=person.0 %}
      	{{ name }}
      {% endwith %}
      
    • two

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

    • 这个别名的作用域只在with语句块内

      <!-- html -->
      {% with person.0 as name %}
      	{{ name }}
      	{{ name }}
      {% endwith %}
      	{{ name }} {# 这一句没执行 #}
      
  5. url标签。 类似reverse()函数。会用到映射时URL指定过的名字进行反转

    <!-- html -->
    <a href="{% url 'book:list' %}"> 列表 </a>
    (# 这里'book:list'是定义在app_name是book的名为list的URL#)
    {# url和'book:list'之间需要空格,也就是说空格是分隔符#}
    
    
    • 传递参数。和reverse()类似

      位置参数和关键字参数不可以同时用。

      # urls.py
      path("detail/<book_id>/", 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))
      
      <!-- html -->
      {# url反转,使用位置参数 #}
      <a href="{% url 'book:detail’ 1 %}"> 页面 </a> {# 位置参数根据URL的变量位置进行排列 #}
      
      {# url反转, 使用关键字参数 #}
      <a href="(% url 'book:detail' book_id=1 %)"> 页面 </a>
      

      如果想要传递查询字符串的参数,必须手动在后面添加

      <!-- html -->
      <a href = "{% url 'book:detal' book_id=1 %}?page=1"> 页面 </a>
      

      如果要传递多个参数,那么通过空格进行分割

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

    • 使用

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

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

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

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

    {% verbatim %}
    	{{ name }}
    {% endverbatim %}}
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值