<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 定義模板 * 模板語言包括 * 變量 * 標簽{百分號 代碼塊 百分號} * 過濾器 * 注釋{\#代碼或HTML\#} ## 變量 * 語法 ```text {{variable}} ``` * 當模板引擎要一個變量,將計算這個變量,然后將結果輸出 * 變量名必須由字母、數字、下劃線\(不能以下劃線開頭\)和點組成 * 當模板引擎遇到點\("."\),會按照下列順序查詢: * 1.字典查詢,例如:key\['value'\] * 2.屬性或方法查詢:例如key.value * 3.數字索引查詢,例如:key\[value\] * 如果變量不存在,模板系統將插入\(空字符串\)"" * 在模板中調用方法時不能傳遞參數 ## 在模板中調用對象的方法 * 在models.py中定義類Grades ```text from django.db import models # Create your models here. class GradesManager(models.Manager): def get_queryset(self): return super(GradesManager,self).get_queryset().filter(isDelete=False) def createGrades(self,name,date,gril,boy,isDel): g = self.model() g.gname = name g.gdate = date g.ggrilnum = gril g.gboynum = boy g.isDelete = isDel return g class Grades(models.Model): gObj = GradesManager() gname = models.CharField(max_length=20) gdate = models.DateField() ggirlnum = models.IntegerField() gboynum = models.IntegerField() isDelete = models.BooleanField(default=False) ``` * 在views.py中傳遞Grades對象 ```text from django.shortcuts import render from django.shortcuts import * # Create your views here. def index(request): context = {'hello':'hello world'} return render(request,'myapp/index.html',context) ``` * 在模板index.html中調用 ```text {{ hello }} ``` ## 標簽 * 語法:{百分號tag百分號} * 作用 * 在輸出中創建文本 * 控制循環或邏輯 * 加載外部信息到模板中供以后的變量使用 * for標簽 ```text {%for ... in ...%} 循環邏輯 {{forloop.counter}}標識當前是第幾次循環 {%empty%} 給出的列表為列表不存在時,執行此處 {%endfor%} ``` * if標簽 ```text {%if ...%} 邏輯 {%elif ...%} 邏輯 {%else%} 邏輯 {%endif%} ``` * comment標簽 ```text {% comment %} 多行注釋 {% endcomment %} ``` * include:加載模板并以標簽內的參數渲染 ```text {%include "myapp/footer.html" %} ``` * url:反向解析 ```text {% url 'app:name' p1 p2%} ``` * crst\_token:這個標簽用于跨站請求偽造保護 ```text {%crsf_token%} ``` * 布爾標簽:and、or、and比or的優先級高 * block、extends:詳見"繼承模板" * autoescape:詳見"HTML"轉義 ## 過濾器 * 語法:,例如,表示將變量name的值變為小寫輸出 * 使用管道符號\\(\\|\\)來應用過濾器 * 通過使用過濾器來改變變量的計算結果 * 可以在if標簽中過濾器結合運算符 ```text if list|length > 1 ``` * 過濾器能夠被"串聯",構成過濾器鏈 ```text name|lower|upper ``` * 過濾器可以傳遞參數,參數使用引號包起來 ```text list:join:"," ``` * default:如果一個變量沒有被提供,或者為fals額或空,則使用默認值,否則使用變量的值 ```text value|default:"沒有初始化" ``` * date:根據給定格式一個date變量格式化 ```text value|date:"Y-m-d" ``` * escape:詳見"HTML轉義" * 詳細的過濾器 * value\|add:number ```text <h1> num = {{num}} </h1> <h1> num = {{num|add:10}} </h1> <h1> num = {{num|add:-5}} </h1> ``` * -- 乘法 * num/1\*5 ```text # num={%widthratio num 1 5%} # num={%widthratio num 5 1%} ``` ## 注釋 * 單行注釋 ```text {#....#} ``` * 注釋可以包含任何模板代碼,有效的或者無效的都可以 ```text {{#{% if a %}bar{%else%}#} ``` * 示例:查詢所有班級,要求奇數行顯示紅色,偶數行顯示為藍色 ```text <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title> Title </title> </head> <body> <ul> {# {{ grades }}#} {% for grade in grades%} {% if forloop.counter|divisibleby:2 %} <li style="background-color: green"> {% else %} <li style="background-color: pink"> {% endif %} {{ grade.gname }} -- {{ grade.gdate }} {%empty%} 目前沒有信息 </li> {% endfor %} </ul> </body> </html> ``` 額外知識點 ```text forloop.counter 索引從 1 開始算 forloop.counter0 索引從 0 開始算 forloop.revcounter 索引從最大長度到 1 forloop.revcounter0 索引從最大長度到 0 forloop.first 當遍歷的元素為第一項時為真 forloop.last 當遍歷的元素為最后一項時為真 forloop.parentloop 用在嵌套的 for 循環中,獲取上一層for 循環的 forloop ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看