您的位置:首页 > 其它

Flask向模板(template)传递多个参数或者把全部的本地参数传递给template

2016-07-18 13:59 399 查看

向模板(template)传递多个参数或者把全部的本地参数传递给template:

1. 传递多个参数给template,直接将参数放在render_template()函数里面,参数间用逗号隔开:

@app.route('/')
def index():
content = '.....'
user='Micheal'
return render_template('index.html', var1=content, var2=user)


template中可以直接使用{{var1}}和{{var2}}直接操作变量。

2. 传递全部的本地变量给template,使用**locals():

@app.route('/')
def index():
content = '.....'
user='Micheal'
return render_template('index.html', **locals())


template中可以直接使用{{content}}和{{user}}直接操作变量。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: