亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

在Django中自定義filter并在template中的使用詳解

瀏覽:5日期:2024-10-04 15:54:09

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 templateregister=template.Library()@register.filterdef get_range(value): return range(value)

上述代碼中定義了一個生成列表的函數,@register.filter表示這個函數是一個過濾器。至此我們的生成列表的過濾器就已經寫好了。接下來我們需要把這個過濾器的庫加載到模板里。

在你想要使用的模板的頂部加上{% load generalfilters %},就可以使用這個過濾器了。

{% for i in 5|get_range_bet_within %} {{i}}{% endfor %}

運行結果

在Django中自定義filter并在template中的使用詳解

補充知識: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中的使用詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Django
相關文章:
主站蜘蛛池模板: 黄色大片aaa | 亚洲人成伊人成综合网久久 | 亚洲精品视频在线观看免费 | 欧美成人久久一级c片免费 欧美成人午夜不卡在线视频 | 国产成人精品一区二区免费 | 亚洲国产综合久久精品 | 麻豆视传媒短视频网站-欢迎您 | 亚洲国产天堂久久九九九 | 在线观看中文字幕国产 | 黄色片s色 | 亚洲精品一区久久狠狠欧美 | 99久久精品国产国产毛片 | 国产精品亚洲片在线不卡 | 国产一区在线免费观看 | 国产午夜在线观看视频 | 国产99视频精品免费视频7 | 国产大战女模特在线视频 | 午夜精品视频在线观看 | 国产精品又黄又爽又色视频 | 国产一区中文字幕 | 日本无卡码免费一区二区三区 | 欧美日韩午夜精品不卡综合 | 韩毛片| 亚洲国产精品综合福利专区 | 国产成人久久777777 | 一级特黄aaa大片在线观看视频 | 天天摸夜夜添久久精品麻豆 | 久久精品国产999久久久 | 亚洲欧美综合网站 | 伊人第四色 | 黄色三级在线 | 91精品小视频 | 亚洲欧美日韩激情在线观看 | 男女啪啪免费观看网站 | 色综合久久精品中文字幕 | 欧美日韩国产58香蕉在线视频 | 精品国产香蕉伊思人在线又爽又黄 | 手机看片自拍自自拍日韩免费 | 欧美唯爱网 全黄性播放 | 精品美女模特在线网站 | 免费的黄色小视频 |