django學習筆記(模板)

django版本:1.6.5


模板文件夾配置: settings.py 中,base_dir下添加:

    TEMPLATE_DIRS = os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/')


模板:

   1. 模板可以是任何文件,也可以是字符串你

 

模板上下文:

   爲一個dict對象,可用於傳遞具體的值到view


模板語法:

   1. 判斷&循環 {% if/else/endif%} {% for ... in .../endfor %}

   2. 模板變量,用 {{ }} 包裹的部分,用於具體的輸出

   3. 特殊模板標籤,

    4. 註釋 :

         1).  {# 單行 #}

         2).  {% commont %}

           行1

           行2

          {% endcommont %}




模板渲染方法:

   1.django.template.Template(str)

   2.django.template.get_template(file_name)

   3.django.shortcuts.render_to_response/render

其中,第三種方式一步到位,直接返回 django.http.HttpResponse對象,是推薦的做法。方式1和2需要另外在用context來render加載的template,並用render的結果返回HttpResponse對象


模板集成:

{% include 'your_template.html' %}


模板繼承:

base.html

{% block  被集成塊名 %} 默認實現 {% endblock %}


sub_template.html

{% extends "base.html" %} 

{% block 要重寫的塊名} 具體實現,可通過 {{block.super}}調用父類內容{% endblock %}



轉載請註明來自:http://blog.csdn.net/lion_awake/article/details/37759173,謝謝


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章