版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/GoodLuckAC/article/details/79077096
1.如何渲染模板:
*模板放在‘templates’文件夹下
*从flask中导入‘render_template’函数
*在视图函数中,使用‘render_template’函数,渲染模板。只需要填写‘template’这个文件夹的路径。
2.模板传参:
*如果只有一个或者少量的参数,直接在‘render_templa’函数添加关键字就行了。
*如果有多个参数的时候,先把参数放到字典里面,放在‘render_template’中,使用二个星号,把字典转化成关键参数传递进去,这样代码更方便管理。
详见代码:
#encoding:utf-8
from flask import Flask,render_template
app = Flask(__name__)
@app.route('/')
def index():
context={
'username':u'二狗子',
'age':18,
'sex':'male'
}
return render_template('index.html',**context )
if __name__ == '__main__':
app.run()