十五、过滤器的基本使用

作者: admin 分类: Flask全栈 发布时间: 2023-04-26 17:11 浏览 169 次
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|过滤器名字}},使用管道符号|

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续整理创作!

发表评论

邮箱地址不会被公开。 必填项已用*标注