Flask010_自定义过滤器

  • 在模板中,通过过滤器实现对变量的处理。
  • 过滤器本质上是 Python 的函数,它会把被过滤器的值当作第1个参数传送给函数。

自定义过滤器

  • 定义
1 def datetime_format(value, format="%Y-%m-%d %H:%M"):
2     return value.strftime(format)
3 
4 
5 app.add_template_filter(datetime_format, 'dformat')
  • 调用
1 @app.route('/datetime')
2 def test():
3     return render_template('test.html', now_time=datetime.now())
  • test.html
复制代码
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6 </head>
 7 <body>
 8 <p>时间:{{ now_time | dformat }}</p>
 9 </body>
10 </html>
复制代码
  • 效果

 

posted @   jason2018  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
历史上的今天:
2019-05-29 循环
2019-05-29 switch实现成绩打等级
2019-05-29 关系运算符
点击右上角即可分享
微信分享提示