当前位置:首页>开发>正文

flask怎样将html中的参数传给视图函数

2023-04-18 04:38:15 互联网 未知 开发

 flask怎样将html中的参数传给视图函数

flask怎样将html中的参数传给视图函数

可以向模板(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}}直接操作变量。

最新文章