from flask import Flask,render_template app = Flask(__name__) #自定义过滤器 @app.template_filter('cut') def cut(value): value = value.replace('xxx,rrr','555') return value @app.route('/') def get(): info = '=============xxx,rrr==============' return render_template('index.html',info = info) if __name__ =='__main__': app.run(debug=True)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>test</title> </head> <body> <h1>自定义过滤器</h1> 过滤前的数据是:{{info}}<br> <!-- 用cut过滤重复的值在显示终端 --> 过滤后的数据是:{{ info | cut }} </body> </html>