在Django中自定義filter并在template中的使用詳解
Django內(nèi)置的filter有很多,然而我們由于業(yè)務(wù)邏輯的特殊要求,有時(shí)候仍然會(huì)不夠用,這個(gè)時(shí)候就需要我們自定義filter來實(shí)現(xiàn)相應(yīng)的內(nèi)容。接下來讓我們從自定義一個(gè)get_range(value)來產(chǎn)生列表的filter開始吧。
首先在你的django app的models.py的同級(jí)目錄建立一個(gè)templatetags的文件夾,并在里面新建一個(gè)init.py的空文件,這個(gè)文件確保了這個(gè)文件夾被當(dāng)做一個(gè)python的包。在添加了templatetags模塊之后,我們需要重新啟動(dòng)服務(wù)器才能使其有效。
polls/ __init__.py models.py templatetags/ __init__.py views.py
然后在templatetags中新建一個(gè)python文件,文件名就是以后需要加載到頁面的自定義庫的名字。在這里我們新建一個(gè)generalfilters.py文件。
polls/ __init__.py models.py templatetags/ __init__.py generalfilters.py views.py
為了讓庫生效,必須在文件里添加一個(gè)模塊級(jí)別的register變量。它是template.Library的實(shí)例,確保了標(biāo)簽和過濾器的有效性。
編輯generalfilters.py,添加
from django import template register=template.Library() @register.filter def get_range(value): return range(value)
上述代碼中定義了一個(gè)生成列表的函數(shù),@register.filter表示這個(gè)函數(shù)是一個(gè)過濾器。至此我們的生成列表的過濾器就已經(jīng)寫好了。接下來我們需要把這個(gè)過濾器的庫加載到模板里。
在你想要使用的模板的頂部加上{% load generalfilters %},就可以使用這個(gè)過濾器了。
{% for i in 5|get_range_bet_within %} {{i}} {% endfor %}
運(yùn)行結(jié)果
補(bǔ)充知識(shí):Django 自定義篩選器:重寫DateFieldListFilter
我就廢話不多說了,大家還是直接看代碼吧!
class MyDateTimeFilter(admin.filters.DateFieldListFilter): def __init__(self, *args, **kwargs): super(MyDateTimeFilter, self).__init__(*args, **kwargs) now = timezone.now() # When time zone support is enabled, convert "now" to the user's time # zone so Django's definition of "Today" matches what the user expects. if timezone.is_aware(now): now = timezone.localtime(now) filter_end_date = now.replace(hour=0, minute=0, second=0, microsecond=0) filter_start_date_for_one_week = filter_end_date - datetime.timedelta(days=7) month_with_day31 = [1,3,5,7,8,10,12] if filter_end_date.month in month_with_day31 and filter_end_date.day == 31 and filter_end_date.month != 3: if filter_end_date.month == 1: filter_start_date_for_one_month = filter_end_date.replace(year=filter_end_date.year-1, month=12) else: filter_start_date_for_one_month = filter_end_date.replace(month=filter_end_date.month-1, day=30) elif filter_end_date.month == 3 and filter_end_date.day in [29, 30, 31]: if is_leap_year(filter_end_date.year): filter_start_date_for_one_month = filter_end_date.replace(month=filter_end_date.month-1, day=29) else: filter_start_date_for_one_month = filter_end_date.replace(month=filter_end_date.month-1, day=28) else: if filter_end_date.month == 1: filter_start_date_for_one_month = filter_end_date.replace(year=filter_end_date.year-1, month=12) else: filter_start_date_for_one_month = filter_end_date.replace(month=filter_end_date.month-1) filter_start_date_for_six_month = '' filter_start_date_for_six_month_month = (filter_end_date.month - 6 + 12) % 12 if filter_start_date_for_six_month_month == 0: filter_start_date_for_six_month_month = 12 if filter_start_date_for_six_month_month in month_with_day31: if filter_end_date.month > 6: filter_start_date_for_six_month = filter_end_date.replace(month=filter_start_date_for_six_month_month) else: filter_start_date_for_six_month = filter_end_date.replace(year=filter_end_date.year-1, month=filter_start_date_for_six_month_month) elif filter_start_date_for_six_month_month == 2: if filter_end_date.day in [29, 30, 31]: if is_leap_year(filter_end_date.year): filter_start_date_for_six_month = filter_end_date.replace(month=filter_start_date_for_six_month_month, day=29) else: filter_start_date_for_six_month = filter_end_date.replace(month=filter_start_date_for_six_month_month, day=28) else: filter_start_date_for_six_month = filter_end_date.replace(month=filter_start_date_for_six_month_month) else: if filter_end_date.day == 31 and filter_end_date.month >6: filter_start_date_for_six_month = filter_end_date.replace(month=filter_start_date_for_six_month_month, day=30) elif filter_end_date.day == 31 and filter_end_date.month <=6: filter_start_date_for_six_month = filter_end_date.replace(year=filter_end_date.year-1, month=filter_start_date_for_six_month_month, day=30) elif filter_end_date.day <31 and filter_end_date.month >6: filter_start_date_for_six_month = filter_end_date.replace(month=filter_start_date_for_six_month_month) else: filter_start_date_for_six_month = filter_end_date.replace(year=filter_end_date.year-1, month=filter_start_date_for_six_month_month) filter_end_date = filter_end_date + datetime.timedelta(days=1) self.links = (( ('------', {}), ('Past week', { self.lookup_kwarg_since: str(filter_start_date_for_one_week), self.lookup_kwarg_until: str(filter_end_date), }), ('Past month', { self.lookup_kwarg_since: str(filter_start_date_for_one_month), self.lookup_kwarg_until: str(filter_end_date), }), ('Past 6 months', { self.lookup_kwarg_since: str(filter_start_date_for_six_month), self.lookup_kwarg_until: str(filter_end_date), }), ('All', {}), ))
以上這篇在Django中自定義filter并在template中的使用詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python游戲開發(fā)之Pygame使用的最全教程分享
Pygame庫是Python中一個(gè)專為游戲開發(fā)設(shè)計(jì)的庫,它提供了大量的功能來幫助開發(fā)者創(chuàng)建各種2D游戲,本文就來和大家分享一下Pygame的具體使用,希望對(duì)大家有所幫助2023-05-05Python函數(shù)式編程中itertools模塊詳解
這篇文章主要介紹了在Python中使用itertools模塊中的組合函數(shù)的教程,來自IBM官方技術(shù)文檔,需要的朋友可以參考下,希望能夠給你帶來幫助2021-09-09python opencv攝像頭的簡(jiǎn)單應(yīng)用
這篇文章主要為大家詳細(xì)介紹了python opencv攝像頭的簡(jiǎn)單應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06利用Python命令行傳遞實(shí)例化對(duì)象的方法
最近在工作中遇到了一個(gè)問題,需要接收啟動(dòng)腳本傳遞過來的實(shí)例化后的對(duì)象,通過在網(wǎng)上查找資料發(fā)現(xiàn)了兩個(gè)方法,文中通過實(shí)例代碼就給大家詳細(xì)介紹了這兩種方法,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-11-11Python使用虛擬環(huán)境(安裝下載更新卸載)命令
這篇文章主要為大家介紹了Python使用虛擬環(huán)境(安裝下載更新卸載)命令,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11python之yield表達(dá)式學(xué)習(xí)
這篇文章主要介紹了python之yield表達(dá)式學(xué)習(xí),python中有一個(gè)略微奇怪的表達(dá)式叫yield expression,本文就來探究一下這是個(gè)什么東西,需要的朋友可以參考下2014-09-09Pycharm配置遠(yuǎn)程調(diào)試的方法步驟
這篇文章主要介紹了Pycharm配置遠(yuǎn)程調(diào)試的方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12