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

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

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

Django內(nèi)置的filter有很多,然而我們由于業(yè)務(wù)邏輯的特殊要求,有時(shí)候仍然會(huì)不夠用,這個(gè)時(shí)候就需要我們自定義filter來(lái)實(shí)現(xiàn)相應(yīng)的內(nèi)容。接下來(lái)讓我們從自定義一個(gè)get_range(value)來(lái)產(chǎn)生列表的filter開(kāi)始吧。

首先在你的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文件,文件名就是以后需要加載到頁(yè)面的自定義庫(kù)的名字。在這里我們新建一個(gè)generalfilters.py文件。

polls/ __init__.py models.py templatetags/ __init__.py generalfilters.py views.py

為了讓庫(kù)生效,必須在文件里添加一個(gè)模塊級(jí)別的register變量。它是template.Library的實(shí)例,確保了標(biāo)簽和過(guò)濾器的有效性。

編輯generalfilters.py,添加

from django import templateregister=template.Library()@register.filterdef get_range(value): return range(value)

上述代碼中定義了一個(gè)生成列表的函數(shù),@register.filter表示這個(gè)函數(shù)是一個(gè)過(guò)濾器。至此我們的生成列表的過(guò)濾器就已經(jīng)寫(xiě)好了。接下來(lái)我們需要把這個(gè)過(guò)濾器的庫(kù)加載到模板里。

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

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

運(yùn)行結(jié)果

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

補(bǔ)充知識(shí):Django 自定義篩選器:重寫(xiě)DateFieldListFilter

我就廢話不多說(shuō)了,大家還是直接看代碼吧!

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è)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Django
相關(guān)文章:
主站蜘蛛池模板: 91视频免费网址 | 亚洲人成综合网站在线 | 国产v日韩v欧美v精品专区 | 一级黄色在线 | 午夜激情视频在线观看 | 国内精品网站 | 久久久亚洲欧洲日产国码二区 | 亚洲黄色网址在线观看 | 三级毛片免费看 | 日韩欧美亚洲综合一区二区 | 毛片在线播放a | 精品成人在线视频 | 国产一区二区免费福利片 | 黄色一级片视频 | 亚洲精品国产专区91在线 | 黄页网址大全免费观看不用 | 欧美婷婷 | 亚洲在线网址 | 亚洲欧洲精品视频在线观看 | 自拍偷拍视频在线观看 | 国产另类图片 | 国产精品一区二区久久不卡 | 嫩草在线视频www免费观看 | 伊人影院中文字幕 | 99久久精彩视频 | 草逼视频免费看 | 在线欧美一级毛片免费观看 | 一级片视频网站 | 日韩中文字幕在线观看 | 男女啪视频大全1000 | 国产色图区 | 青草色视频 | 欧美高清不卡 | 黄色在线播放网址 | 一级一级黄色片 | 香蕉免费一级视频在线观看 | 国产性生活| 久久五月视频 | 日韩在线一区二区 | 午夜在线精品不卡国产 | 亚洲欧洲小视频 |