在Django中自定義filter並在template中的使用詳解
阿新 • • 發佈:2020-05-20
Django內建的filter有很多,然而我們由於業務邏輯的特殊要求,有時候仍然會不夠用,這個時候就需要我們自定義filter來實現相應的內容。接下來讓我們從自定義一個get_range(value)來產生列表的filter開始吧。
首先在你的django app的models.py的同級目錄建立一個templatetags的資料夾,並在裡面新建一個init.py的空檔案,這個檔案確保了這個資料夾被當做一個python的包。在添加了templatetags模組之後,我們需要重新啟動伺服器才能使其有效。
polls/ __init__.py models.py templatetags/ __init__.py views.py
然後在templatetags中新建一個python檔案,檔名就是以後需要載入到頁面的自定義庫的名字。在這裡我們新建一個generalfilters.py檔案。
polls/ __init__.py models.py templatetags/ __init__.py generalfilters.py views.py
為了讓庫生效,必須在檔案裡新增一個模組級別的register變數。它是template.Library的例項,確保了標籤和過濾器的有效性。
編輯generalfilters.py,新增
from django import template register=template.Library() @register.filter def get_range(value): return range(value)
上述程式碼中定義了一個生成列表的函式,@register.filter表示這個函式是一個過濾器。至此我們的生成列表的過濾器就已經寫好了。接下來我們需要把這個過濾器的庫載入到模板裡。
在你想要使用的模板的頂部加上{% load generalfilters %},就可以使用這個過濾器了。
{% for i in 5|get_range_bet_within %} {{i}} {% endfor %}
執行結果
補充知識: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,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),('Past 6 months',{ self.lookup_kwarg_since: str(filter_start_date_for_six_month),('All',))
以上這篇在Django中自定義filter並在template中的使用詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。