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, 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中的使用详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]
