渲染模板所用的包render_template
from flask import Flask,render_template
模板变量的使用
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)
if __name__ == '__main__':
app.run(debug=True)
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<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>
</body>
</html>
image.png
运行结果
网友评论