1. 程式人生 > >Python大神 - Django(深層學習)-- 模板語言

Python大神 - Django(深層學習)-- 模板語言

del xxx display nbsp ges safe gif ons city

寄語:

模板語言:模版的創建過程,對於模版,其實就是讀取模版(其中嵌套著模版標簽),然後將 Model 中獲取的數據插入到模版中,最後將信息返回給用戶。

模板(template)中也有自己的語言,該語言可以實現數據展示

一、繼承(include,extend)

a.完全一致   

  {% include xxx.html %}

b.繼承模板,設計自己特立的部分({% block block_name %}{% endblock %})

  母版:{% block title %}{% endblock %}

  子版:{% extend xxx.html%}
     {% block block_name %}{% endblock %}

二、數據獲取

  獲取單個:{{ item }}

  循環:{% for item in item_list %}  <a>{{ item }}</a>  {% endfor %}
     forloop.counter  # 計算器,從1開始,forloop.counter

     forloop.first # 第一次循環      forloop.last   # 最後一次循環

三、判斷

        {% if ordered_warranty %}  {% else %} {% endif %}
        {% if not ordered_warranty %} {% else
%} {% endif %} {% if sort == courses or sort == org or sort == teacher % } {% else %} {% endif %} {% if sort == courses and sort == org % } {% else %} {% endif %} 註:or與and不可放進同一邏輯裏 {% if sort == courses % } {% else %} {% endif %} {
% ifequal sort courses %}active{% endifequal %}

四、模板語言裏提供的方法

 1   2)stringformat(數據類型的轉換) 4       # 數據類型的轉換
 5       {% ifequal city.id|stringformat:i city_id %}active2{% endifequal %} 
 6             
 7     3)分割(路徑分割)--slice
 8         {% if request.path|slice:7  == /course %}{% endif %}
 9         
10     4)顯示有choice字段的命名(get_degree_display)
11         {{ request.user.get_degree_display }}
12         
13     5)divisibleby標簽的意義是用後面的參數去除,除盡為True,否則為False。
14         {% if forloop.counter|divisibleby:2 %}
15         
16     6)代碼表示:5/1 *100,返回500,widthratio需要三個參數,它會使用 參數1/參數2*參數3,所以要進行乘法的話,就將參數2=1即可 Django模版除法
17         {%  widthratio 5 1 100 %}
18         
19     7)定制日期顯示模式
20         {{ item.event_start|date:"Y-m-d H:i:s"}}
21         
22     823         {{ bio|truncatewords:"30" }}
24         
25     9)首字母大寫
26         {{ my_list|first|upper }} 
27         
28     10)變小寫
29         {{ name|lower }}

    11)放置CSRF攻擊
      {% csrf_token %} # form表單使用
      xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}"); # 用POST提交必須請求頭帶csrf_token # post 方法下使用

五、自定義simple_tag

技術分享
a、在app中創建templatetags模塊

b、創建任意 .py 文件,如:xx.py
    #!/usr/bin/env python
    #coding:utf-8
    from django import template
    from django.utils.safestring import mark_safe
       
    register = template.Library()
       
    @register.simple_tag
    def my_simple_time(v1,v2,v3):
        return  v1 + v2 + v3
       
    @register.simple_tag
    def my_input(id,arg):
        result = "<input type=‘text‘ id=‘%s‘ class=‘%s‘ />" %(id,arg,)
        return mark_safe(result)
    
c、在使用自定義simple_tag的html文件中導入之前創建的 xx.py 文件名
    {% load xx %}

d、使用simple_tag
    {% my_simple_time 1 2 3%}
    {% my_input id_username hide%}

e、在settings中配置當前app,不然django無法找到自定義的simple_tag  
    INSTALLED_APPS = (
        django.contrib.admin,
        django.contrib.auth,
        django.contrib.contenttypes,
        django.contrib.sessions,
        django.contrib.messages,
        django.contrib.staticfiles,
        app01,
    )
自定義方法--步驟

Python大神 - Django(深層學習)-- 模板語言