十五、过滤器的基本使用
app.py文件 from flask import Flask, render_template, url_for app = Flask(__name__) @app.route('/') def index(): # put application's code here return render_template('index.html', position=-9) if __name__ == '__main__': app.run(debug=True, host='0,0,0,0', port=5000)
模板文件:index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <p>位置的绝对值是:{{ position|abs }}</p> </body> </html>
# 常用过滤器:
# 1、在模板中对变量进行处理,需要类似于python中的函数一样,将这个值传入函数中,然后做一些操作。在模板中,过滤器相当于一个函数,把当前的变量传入过滤器,根据过滤器的功能,再返回相应的值,再讲结果渲染到页面中。
# 2、{{variable|过滤器名字}},使用管道符号|