成熟丰满熟妇高潮XXXXX,人妻无码AV中文系列久久兔费 ,国产精品一国产精品,国精品午夜福利视频不卡麻豆

您好,歡迎來到九壹網(wǎng)。
搜索
您的當(dāng)前位置:首頁五、自定義過濾器及標(biāo)簽

五、自定義過濾器及標(biāo)簽

來源:九壹網(wǎng)

1 關(guān)于自定義

  • 自定義的引入

?

?

templatestemplatetags
存放模板的目錄存放自定義標(biāo)簽及過濾器的目錄
?

2 代碼布局

  • 某個(gè)app特有的

  • 定義復(fù)用

    -創(chuàng)建一個(gè)新的app,將他們定義在新的app中,在INSTALL_APPS注冊,然后就可以應(yīng)用

3 自定義模板過濾器

自定義過濾器就是一個(gè)帶有一個(gè)或兩個(gè)參數(shù)的Python 函數(shù):

- (輸入的)變量的值 —— 不一定是字符串形式。

- 參數(shù)的值 —— 可以有一個(gè)初始值,或者完全不要這個(gè)參數(shù)。

?

  • 注冊自定義過濾器:
1 django.template.Library.filter()
  • 例子

    1.?在customer_filters.py文件中自定義to_male的過濾器功能,并注冊

 1 from django.template import Library
 2 
 3 register = Library()
 4 
 5 @register.filter()
 6 def to_male(value, arg='zh'):
 7     map = {
 8         'zh': ('', ''),
 9         'en': ('female', 'male')
10     }
11     return map[arg][value]

    2.?使用自定義過濾器

 1 {% load static %}
 2 {% load customer_filters %}
 3 
 4 <table class="table">
 5     <thead>
 6       <tr>
 7           <th>序號(hào)</th>
 8           <th>姓名</th>
 9           <th>性別</th>
10           <th>年齡</th>
11           <th>課程</th>
12       </tr>
13     </thead>
14     <tbody>
15     {% for i in students %}
16       <tr {% if i.gender == 0 %}style="color: red"{% endif %}>
17           <th>{{ forloop.counter }}</th>
18           <th><a href="{% url 'teacher:detail' i.id %}">{{ i.name }}</a></th>
19           <th>{{ i.gender|to_male:'en' }}</th>
20           <th>{{ i.age }}</th>
21           <th>{% show_list_as_ul i.course style='link' %}</th>
22       </tr>
23     {% endfor %}
24     </tbody>
25 </table>

    3. 效果圖

4 自定義標(biāo)簽

分為兩類:

- 簡單標(biāo)簽 django.template.Library.simple_tag()

- 包含標(biāo)簽 django.template.Library.inclusion_tag()

tag()方法有兩個(gè)參數(shù):

  1. 模板標(biāo)記的名稱 - 字符串。 如果省略,將使用編譯函數(shù)的名稱。

  2. 編譯的函數(shù) – 一個(gè)Python函數(shù)(不要把函數(shù)名寫成字符串)

與過濾器注冊一樣,也可以將其用作裝飾器。

4.1 簡單標(biāo)簽

  • 例子

    1.?在customer_tags.py文件中定義標(biāo)簽的功能,注冊標(biāo)簽時(shí)使用takes_context 參數(shù),則可以使用從上下文中傳入的參數(shù).

1 from datetime import datetime
2 from django.template import Library
3 
4 register = Library()
5 
6 @register.simple_tag(name='current', takes_context=True)
7 def current_time(context):
8     return datetime.now().strftime(context['format_str'])

    2.?在index.html文件中使用標(biāo)簽

1 {% load static %}
2 {% load customer_tags %}
3 
4 <h1>當(dāng)前時(shí)間:{% current %}</h1>

4.2 包含標(biāo)簽

  • 例子

    1.?在customer_tags.py文件中定義標(biāo)簽的功能

1 from django.template import Library
2 
3 register = Library()
4 
5 @register.inclusion_tag('teacher/show_list_as_ul.html')
6 def show_list_as_ul(value, style):
7     return {'ls': value, 'style': style}

    2.?在show_list_as_ul.html文件中存放功能代碼

 1 {% if style == 'button' %}
 2     <div class="list-group">
 3         {% for l in ls %}
 4             <button type="button" class="list-group-item">{{ l }}</button>
 5         {% endfor %}
 6     </div>
 7 {% elif style == 'link' %}
 8     <div class="list-group">
 9         {% for l in ls %}
10             <a href="#" class="list-group-item">{{ l }}</a>
11         {% endfor %}
12     </div>
13 {% else %}
14     <ul class="list-group">
15     {% for l in ls %}
16     <li class="list-group-item">{{ l }}</li>
17     {% endfor %}
18     </ul>
19 {% endif %}

    3.?在index.html文件中使用包含標(biāo)簽

 1 {% load static %}
 2 {% load customer_tags %}
 3 
 4 <table class="table">
 5     <thead>
 6       <tr>
 7           <th>序號(hào)</th>
 8           <th>姓名</th>
 9           <th>性別</th>
10           <th>年齡</th>
11           <th>課程</th>
12       </tr>
13     </thead>
14     <tbody>
15     {% for i in students %}
16       <tr {% if i.gender == 0 %}style="color: red"{% endif %}>
17           <th>{{ forloop.counter }}</th>
18           <th><a href="{% url 'teacher:detail' i.id %}">{{ i.name }}</a></th>
19           <th>{{ i.gender|to_male:'en' }}</th>
20           <th>{{ i.age }}</th>
21           <th>{% show_list_as_ul i.course style='link' %}</th>
22       </tr>
23     {% endfor %}
24     </tbody>
25 </table>

    4.?視圖函數(shù)views.py

 1 def new_index(request):
 2     student_table = [
 3         {'name': '張三', 'gender': 1, 'age': 21, 'id': 56, 'course': ['滲流力學(xué)', '油層物理', "油藏工程"]},
 4         {'name': '李四', 'gender': 0, 'age': 18, 'id': 49, 'course': ['高等滲流力學(xué)', '油氣田開發(fā)', "高等油藏工程"]},
 5         {'name': '王五', 'gender': 1, 'age': 25, 'id': 106, 'course': ['數(shù)值分析', '模糊數(shù)學(xué)', "應(yīng)用統(tǒng)計(jì)"]},
 6         {'name': '趙六', 'gender': 0, 'age': 29, 'id': 21, 'course': ['凝析氣藏', '油氣藏流體', "多相流理論"]},
 7     ]
 8     format_str = '%Y-%m-%d %H:%M:%S'
 9     return render(request, 'teacher/index.html',
10                   context={
11                       'students': student_table,
12                       'format_str': format_str,
13                   }
14                   )

    5. 效果圖

5 總結(jié)

轉(zhuǎn)載于:https://www.cnblogs.com/weixiansheng/p/10709638.html

因篇幅問題不能全部顯示,請點(diǎn)此查看更多更全內(nèi)容

Copyright ? 2019- 91gzw.com 版權(quán)所有 湘ICP備2023023988號(hào)-2

違法及侵權(quán)請聯(lián)系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市萬商天勤律師事務(wù)所王興未律師提供法律服務(wù)