【发布时间】:2021-07-03 13:00:32
【问题描述】:
所以我有这个 Flask 应用程序,我需要将特定用户传递给所有路由,因为应用程序中的所有页面都需要特定用户来呈现模板。我不想像正常那样通过用户
return render_template('index.html',user=user)
因为我必须对 render_templates 中的所有路由重复相同的操作。 请问我该怎么做?
【问题讨论】:
-
变量
user是从哪里来的或者是在哪里设置的?在函数内部还是全局? -
用户实际上是全局的,因为它是导入的。如何通过 render_template 函数将其发送到所有页面的问题。更像是 render_template 的默认参数
-
你可以使用全局变量,它在模板中自动可用flask.palletsprojects.com/en/2.0.x/tutorial/templates/…