电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

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

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

瀏覽:4日期: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
相關文章:
主站蜘蛛池模板: 物流之家新闻网-最新物流新闻|物流资讯|物流政策|物流网-匡匡奈斯物流科技 | 酒水灌装机-白酒灌装机-酒精果酒酱油醋灌装设备_青州惠联灌装机械 | 伺服电机维修、驱动器维修「安川|三菱|松下」伺服维修公司-深圳华创益 | 楼承板设备-楼承板成型机-免浇筑楼承板机器厂家-捡来 | 热缩管切管机-超声波切带机-织带切带机-无纺布切布机-深圳市宸兴业科技有限公司 | 网站建设,北京网站建设,北京网站建设公司,网站系统开发,北京网站制作公司,响应式网站,做网站公司,海淀做网站,朝阳做网站,昌平做网站,建站公司 | 丝杆升降机-不锈钢丝杆升降机-非标定制丝杆升降机厂家-山东鑫光减速机有限公司 | 铝镁锰板_铝镁锰合金板_铝镁锰板厂家_铝镁锰金属屋面板_安徽建科 | ph计,实验室ph计,台式ph计,实验室酸度计,台式酸度计 | 天一线缆邯郸有限公司_煤矿用电缆厂家_矿用光缆厂家_矿用控制电缆_矿用通信电缆-天一线缆邯郸有限公司 | 柔软云母板-硬质-水位计云母片组件-首页-武汉长丰云母绝缘材料有限公司 | 粘度计NDJ-5S,粘度计NDJ-8S,越平水分测定仪-上海右一仪器有限公司 | 温州食堂承包 - 温州市尚膳餐饮管理有限公司 | 江苏农村商业银行招聘网_2024江苏农商行考试指南_江苏农商行校园招聘 | 纯化水设备-纯水设备-超纯水设备-[大鹏水处理]纯水设备一站式服务商-东莞市大鹏水处理科技有限公司 | 派克防爆伺服电机品牌|国产防爆伺服电机|高低温伺服电机|杭州摩森机电科技有限公司 | 成都网站建设制作_高端网站设计公司「做网站送优化推广」 | 尚为传动-专业高精密蜗轮蜗杆,双导程蜗轮蜗杆,蜗轮蜗杆减速机,蜗杆减速机生产厂家 | 北京公寓出租网-北京酒店式公寓出租平台 | 众能联合-提供高空车_升降机_吊车_挖机等一站工程设备租赁 | 转向助力泵/水泵/发电机皮带轮生产厂家-锦州华一精工有限公司 | 网站建设-临朐爱采购-抖音运营-山东兆通网络科技 | 招商帮-一站式网络营销服务|互联网整合营销|网络推广代运营|信息流推广|招商帮企业招商好帮手|搜索营销推广|短视视频营销推广 | 橡胶接头|可曲挠橡胶接头|橡胶软接头安装使用教程-上海松夏官方网站 | 防腐木批发价格_深圳_惠州_东莞防腐木厂家_森源(深圳)防腐木有限公司 | 一体化污水处理设备_生活污水处理设备_全自动加药装置厂家-明基环保 | 吉祥新世纪铝塑板_生产铝塑板厂家_铝塑板生产厂家_临沂市兴达铝塑装饰材料有限公司 | 贴片电容-贴片电阻-二三极管-国巨|三星|风华贴片电容代理商-深圳伟哲电子 | 老房子翻新装修,旧房墙面翻新,房屋防水补漏,厨房卫生间改造,室内装潢装修公司 - 一修房屋快修官网 | 闪电优家-卫生间防水补漏_酒店漏水渗水维修_防水堵漏公司 | 不锈钢闸阀_球阀_蝶阀_止回阀_调节阀_截止阀-可拉伐阀门(上海)有限公司 | 环讯传媒,永康网络公司,永康网站建设,永康小程序开发制作,永康网站制作,武义网页设计,金华地区网站SEO优化推广 - 永康市环讯电子商务有限公司 | 扫地车厂家-山西洗地机-太原电动扫地车「大同朔州吕梁晋中忻州长治晋城洗地机」山西锦力环保科技有限公司 | 捷码低代码平台 - 3D数字孪生_大数据可视化开发平台「免费体验」 | SMN-1/SMN-A ABB抽屉开关柜触头夹紧力检测仪-SMN-B/SMN-C-上海徐吉 | 南昌旅行社_南昌国际旅行社_南昌国旅在线 | 棕刚玉-白刚玉厂家价格_巩义市东翔净水材料厂 | 广东风淋室_广东风淋室厂家_广东风淋室价格_广州开源_传递窗_FFU-广州开源净化科技有限公司 | POS机官网 - 拉卡拉POS机免费办理|官网在线申请入口 | 【ph计】|在线ph计|工业ph计|ph计厂家|ph计价格|酸度计生产厂家_武汉吉尔德科技有限公司 | 集装箱展厅-住人集装箱住宿|建筑|房屋|集装箱售楼处-山东锐嘉科技工程有限公司 |