标签:timestamp day src oct load short class com temp
views.py 代码
1 from django.shortcuts import render,HttpResponse,redirect,reverse 2 from datetime import datetime 3 4 def test_view(request): 5 context = { 6 ‘today1‘:datetime.now(), 7 } 8 return render(request, ‘test.html‘, context=context)
自定义的过滤器代码
1 from django import template 2 from datetime import datetime 3 4 register = template.Library() 5 @register.filter(‘my_time‘) 6 # my_time,重命名,在HTML中调用时用这个名字,如果没有传参就使用函数名time_since 7 def time_since(value): 8 # 过滤器最多只有两个参数 9 # 过滤的第一个参数永远都是被过滤的那个参数(也就是竖线左边的那个参数) 10 if not isinstance(value, datetime): 11 return value 12 now_time = datetime.now() 13 timestamp = (now_time - value).total_seconds() 14 # 获取时间差的秒数 15 if timestamp < 60: 16 return ‘刚刚‘ 17 else: 18 return ‘好一会儿了...‘
test.html 代码
1 <!DOCTYPE html> 2 {% load my_filters %} 3 <html lang="en"> 4 <head> 5 <meta charset="UTF-8"> 6 <title></title> 7 </head> 8 <body> 9 {{ today1|my_time }} 10 </body> 11 </html>
标签:timestamp day src oct load short class com temp
原文地址:https://www.cnblogs.com/kogmaw/p/12449863.html