In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the example analysis of the implementation process of Python custom computing time filter, which is very detailed and has a certain reference value. Interested friends must read it!
When writing a custom filter, because django.template.Library.filter () itself can be used as a decorator, you can use:
Register = django.template.Library () @ register.filter instead of register.filter ("filter name", "function name")
If you use @ register.filter to register a custom filter and do not pass any parameters, then the default filter name and function name are the same, of course, you can also modify it, as long as @ register.filter ("filter name"), the filter name will be changed, and you can use the custom filter in the DTL template.
Define the time calculation filter when the time is displayed, the display rule of time: how long is the interval between time distance and now
If the interval is less than 1 minute, then it's like "just now." if the interval is more than 1 minute and less than 1 hour, then it shows "xx minute before" if it is greater than 1 hour and less than 24 hours, then it shows "xx hour before" if it is greater than 24 hours and less than 30 days, then it shows "xx days ago". Otherwise, it shows the specific time.
The sample code is as follows:
Custom filter file my_fliter.py
@ register.filter () def time_since (value): # first judge the incoming time. If it is of type datetime, it can be compared with the current time. # if it is not of type datetime, directly return value if not isinstance (value,datetime): return value # if it can be reached here, it represents the type of datetime. # timedelay.total_seconds () attribute now = datetime.now () timestamp = (now-value) .total_seconds () if timestamp
< 60: return "刚刚" elif timestamp >= 60 and timestamp
< 60*60: # 在python3中如果两数相除,有余数的话,就会保持小数,这个时候我们就可以使用int()函数,进行转换 minutes = int(timestamp/60) return "%s分钟前" % minutes elif timestamp >= 60'60 and timestamp
< 60*60*24: hours = int(timestamp/60/60) return "%s小时前" % hours elif timestamp >= 60 "60" 24 and timestamp < 60 "60" 24 "30: days = int (timestamp/60/60/24) return"% s days ago "% days else: return value.strftime ("% Y/%m/%d% Hpurs% M ")
The view function is defined in views.py and a time is constructed:
From django.shortcuts import renderfrom datetime import datetimedef index (request): context= {'time': datetime (year=2019,month=1,day=16,hour=23,minute=44,second=0)} return render (request,'index.html',context=context)
Make the call in index.html:
{# if you want to use a custom filter, you must first import it. The name of the import is the file name of the custom filter #} {# you must install app into the settings.py file #} {% load my_fliter%} Title {{time | time_since}}
The above is all the contents of the article "sample Analysis of the implementation process of Python Custom Computing time filter". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.