python过滤器
2021/9/16 12:04:50
本文主要是介绍python过滤器,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
from flask import Flask,render_template #render 模板引擎 #过滤器是个函数,也可以在后台自己设置 #创建过滤器首先要先注册 # 一个函数未定义使用会报错,所以要先定义不然会爆红 # #return返回的结果会传到前端页面,前端页面也会吧{{}}括号里面的数据传回后端 # def my_filter(n): # print(n) # return 11 app = Flask(__name__) # app.add_template_filter(my_filter,"m"
#通过装饰器的形式创建过滤器,如果把一个函数装饰成 # 一个过滤器需要用注册app的形式,要把位置放到app的位置下面 @app.template_filter("m") # def my_filter(n): # print(n) # return 11 def my_filter(n): try: n= int(n) except Exception as e: return "非法" return n+10 前端代码:
{{ "a"|m }}<br> 运行结果
@app.route("/") def index(): return render_template("index.html", a=1, b=[11,222,333] ) 前端代码
{{ a }}<br> {{ a + 10}}<br> {{ b[0] }}<br> 运行显示为
if __name__ == '__main__': app.run()
这篇关于python过滤器的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-18初探Python股票自动化交易:入门指南
- 2024-09-18Python量化入门:轻松掌握量化分析基础与实战
- 2024-09-18Python量化交易:入门指南与实践
- 2024-09-18Python量化交易:入门指南与实战技巧
- 2024-09-14Python人工智能项目实战:从零开始的实践指南
- 2024-09-14探索Python人工智能资料:初学者的指南
- 2024-09-14Python人工智能资料:初学者的全面指南
- 2024-09-13Matplotlib入门:轻松绘制Python数据可视化图表
- 2024-09-13Python人工智能:初学者的入门指南
- 2024-09-13Python人工智能:轻松入门与实践