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)

界面

注意:这里不用导入文件只需要写html的文件名就可以,使用**来解码data字典数据

posted @ 2021-10-08 11:44  Tester-**  阅读(188)  评论(0编辑  收藏  举报