Python大神 - Django(深層學習)-- 範本語言

來源:互聯網
上載者:User

標籤:counter   string   teacher   clu   time   course   mod   count   star   

寄語:範本語言:模版的建立過程,對於模版,其實就是讀模數版(其中嵌套著模版標籤),然後將 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     8)23         {{ 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(深層學習)-- 範本語言

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.