"""
简单总结:
1. 有时候我们想要在模版中对一些变量进行处理,那么就必须需要类似于Python中的函数一样,可以将这个值传到函数中,然后做一些操作。
在模版中,过滤器相当于是一个函数,把当前的变量传入到过滤器中,然后过滤器根据自己的功能,再返回相应的值,之后再将结果渲染到页面中。

2. 基本语法:`{{ variable|过滤器名字 }}`。使用管道符号`|`进行组合。
"""
from flask import Flask,render_template

app = Flask(__name__)

# #【1】过滤器的基本使用
# @app.route('/')
# def hello_world():
#     return render_template('index.html',postion=-1)

#【2】default过滤器的基本使用
@app.route('/')
def hello_world():
    context={
        'postion':'-1'
    }
    return render_template('index.html',**context)


if __name__ == '__main__':
    app.run(debug=True)

下面是index

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h3>过滤器的基本使用</h3>
    <p>位置的绝对值为【未使用过滤器】:{{ postion }}</p>
    <p>位置的绝对值为【使用过滤器】:{{ postion|abs }}</p>
    <hr>
    <h3>default过滤器的使用</h3>
    {# 如果取的到就取什么,如果取不到过滤器就会生效 #}
 <p>个性签名【使用过滤器】:{{ signature|default("此人很烂") }}</p>
</body>
</html>

报错:TypeError: bad operand type for abs(): 'str'如下图2019-09-08.png这个是什么意思?类型错误?没添加default时候好使,添加后就报错了。??


相关课程:Python 全系列>第八阶段:轻量级Web开发利器-Flask框架>Flask之Jinja2模版>转义字符过滤器