1. 程式人生 > >Django——模板標籤 模板的繼承與引用

Django——模板標籤 模板的繼承與引用

Django

  1. 模板標籤
  2. 常用標籤
  3. 模板的繼承與引用

 

 


 

 

模板標籤

  • 標籤在渲染的過程中提供任意的邏輯
  • 標籤語法: 由%}和 {% 來定義的,例如:{%tag%} {%endtag%}
  • 這個定義是刻意模糊的。 例如,一個標籤可以輸出內容,作為控制結構,例如“if”語句或“for”迴圈從資料庫中提取內容,甚至可以訪問其他的模板標籤。

 

 

常用的標籤

配置好url和檢視

 views.py

# 模板標籤
def tem_tags(request):
    
return render(request, 'book/tags.html', context={ 'str1': 'Django', 'str2': 'Flask', 'fruit_list': ['apple', 'pear', 'orange'], 'num_list': ['one', 'two', 'three'], "whats_your_name": 'whoaU?', 'html': '<h1>hello world</h1>' })

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>模板標籤</title>
    <style>
    </style>
</head>
<body>
<p></p>
if elif else
{% if str1 == 'tornado' %}
    這是tornado課程
{% elif str1 == str2 %}
    這是scrapy課程
{% else %}
    這是{{ str1 }}課程
{% endif %}
<br> ifnotequal ifequal {% ifnotequal str1 str2 %} {{ str1 }}{{ str2}}是兩個不同的課程 {% endifnotequal %} <br> {% for foo in fruit_list %} {{ foo }}<br> {% endfor %} <br> forloop.counter0 {% for foo in fruit_list %} {{ forloop.counter }} {% if forloop.counter0 == 1 %} {{ foo }}是梨子 <br> {% elif forloop.counter0 == 0 %} {{ foo }}是蘋果 <br> {% else %} {{ foo }}是橘子 {% endif %} {% endfor %} <br> first last {% for foo in fruit_list %} {% if forloop.first %} first is {{ foo }} <br> {% endif %} {% if forloop.last %} last is {{ foo }} <br> {% endif %} {% endfor %} <br> forloop.parentloop. {% for foo in num_list %} {% for fo in fruit_list %} {% if forloop.parentloop.counter0 == 1 %} {{ foo }} better {{ fo }} <br> {% else %} {{ foo }} {{ fo }} <br> {% endif %} {% endfor %} {% endfor %} <br> url <a href="{% url 'static' %}">跳轉到靜態檔案演示頁面(引數後面用空格隔開)</a> <br> with {% with whats_your_name as o %} {{ o }}<br> {% endwith %} <br> autoescape {% autoescape on %} {{ html }} {% endautoescape %} <br> {% autoescape off %} {{ html }} {% endautoescape %} </body>

 頁面結果:

 


 

 

 模板的繼承與引用

  • Django模版引擎中最強大也是最複雜的部分就是模版繼承了。 模版繼承可以讓你建立一個基本的“骨架”模版,它包含您站點中的全部元素,並且可以定義能夠被子模版覆蓋的 blocks 。
  • 模板繼承使用extends標籤實現。通過使用block來給子模板開放介面。
  • 1、extends必須是模板中的第一個出現的標籤。
  • 2、子模板中的所有內容,必須出現在父模板定義好的block中,否則django將不會渲染。
  • 3、如果出現重複程式碼,就應該考慮使用模板。
  • 4、儘可能多的定義block,方便子模板實現更細的需求。
  • 5、如果在某個block中,要使用父模板的內容,使用block.super獲取。

在模板資料夾中新建兩個html檔案

base.html檔案中:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{% block title %}base{% endblock %}</title>
    <style>
    </style>
</head>
<body>
  {% block content %}
      <p>這是基類模板檔案</p>
  {% endblock %}
  {% block last %}
      <p>這是結尾的模板內容</p>
  {% endblock %}
</body>
</html>

 index_1.html檔案中:

{% extends 'inheritance_reference/base.html' %}

 

然後配置到url和view檢視將index_1渲染出來:

現在我們來更改block裡面的內容,做成我們自己的東西,將index_1.py中對應名字的block內容進行更改

{% extends 'inheritance_reference/base.html' %}
{% block title %}
    我自己的網頁
{% endblock %}
{% block content %}
    <p>這是我自己的中心內容,不是繼承來的</p>
{% endblock %}
{% block last %}
    <p>這是我自己的結尾內容,不是繼承來的</p>
{% endblock %}

 再渲染出來檢視

接下來我們來繼承父類base中的內容,並加上我們自己的內容

{% extends 'inheritance_reference/base.html' %}
{% block title %}
    我自己的網頁
{% endblock %}
{% block content %}
    {{ block.super }}
    <p>這是我自己的中心內容,不是繼承來的</p>
{% endblock %}
{% block last %}
    {{ block.super }}
    <p>這是我自己的結尾內容,不是繼承來的</p>
{% endblock %}

現在再base中的block外面新增一句話,在index_1中也新增一句話

base.py

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{% block title %}base{% endblock %}</title>
    <style>
    </style>
</head>
<body>
    {% block content %}
        <p>這是基類模板檔案</p>
    {% endblock %}
    {% block last %}
        <p>這是結尾的模板內容</p>
    {% endblock %}
<p>這是base沒有block包括的內容</p>
</body>
</html>

 

 index_1.py中:

{% extends 'inheritance_reference/base.html' %}
{% block title %}
    我自己的網頁
{% endblock %}
{% block content %}
    {{ block.super }}
    <p>這是我自己的中心內容,不是繼承來的</p>
{% endblock %}
{% block last %}
    {{ block.super }}
    <p>這是我自己的結尾內容,不是繼承來的</p>
{% endblock %}
<p>這是index_1中沒有block包括的內容</p>

 

 檢視結果:

子模板中的所有內容,必須出現在父模板定義好的block中,否則django將不會渲染。