flask(14):render_template 渲染模板的使用
需求场景
怎么和前端一起渲染数据呢
结构
新建一个文件夹templates->index.html页面
index.html页面数据
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<p>hello {{name}}</p>
<p>hello {{age}}</p>
<p>hello {{my_dict['city']}}</p>
<p>hello {{my_list[0]+my_list[1]}}</p>
</head>
<body>
</body>
</html>
代码
from flask import Flask,render_template
app=Flask(__name__)
@app.route('/index')
def index():
data={
'name':'lh',
'age':18,
'my_dict':{
'city':'gz',
'tel':'ios'
},
'my_list':[1,2,3]
}
return render_template('index.html',**data)
if __name__ == '__main__':
app.run(debug=True)