from flask import Flask,render_template
app = Flask(__name__)
@app.route("/index")
def index():
data = {
"name":"python",
"age":18,
"my_dict":{"city":"sz"},
"my_list":[1,2,3,4,5],
"my_int":0
}
return render_template("index.html",**data)
def list_step_2(li):
"""自定义的过滤器"""
return li[::2]
# 注册过滤器,添加模板过滤器,第一个参数为方法,第二个参数为过滤器别名
# 用在index.html上
app.add_template_filter(list_step_2,"li2")
if __name__ == '__main__':
app.run(debug=True)
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<hr>
<p>name = {{ name }}</p>
<p>age = {{ age }}</p>
<p>my_dict :city = {{ my_dict["city"] }}</p>
<p>my_dict :city = {{ my_dict.city }}</p>
<p>my_list[my_int] : {{ my_list[my_int] }}</p>
<p>my_list[0] + my_list[1] = {{ my_list[0] + my_list[1] }}</p>
<p>{{ "hello" + "python" }}</p>
<p>a{{ " flask world " | trim | upper }}a</p>
</hr>
<p>{{ my_list | li2 }}</p>
</body>
</html>
结果
image.png
网友评论