问题描述
由于我是flask/python的新手,可能不是准确的标题.我正在开发一种内部工具,供不同团队使用.每个团队都有不同的部署阶段,例如alpha
,beta|test
,prod
,还具有多个区域,例如NA
,EU
,AP
等...
Probably not the accurate title since i am new to flask/python. I am working on an internal tool which will be used by different teams. Each team has different stages of their deployments e.g., alpha
, beta|test
, prod
and they also have multiple regions e.g., NA
, EU
, AP
etc ...
现在,当我使用redirect_template
时,我将发送stage
和region
作为变量,然后在模板中使用它们.但是,对每个redirect_template
进行操作都比较麻烦.有什么更好的办法吗?
Right now i when i am using redirect_template
i am sending stage
and region
as variable which are then used in templates. However, doing for every redirect_template
is kind of cumbersome. Is there any better approach to this?
推荐答案
我假设您的Flask
对象的名称为app
(即app = Flask(__name__)
).
I assume your Flask
object's name is app
(i.e., app = Flask(__name__)
).
在app
初始化之后立即放置以下代码.
Place the below code right after the app
is initialized.
@app.context_processor
def inject_stage_and_region():
return dict(stage="alpha", region="NA")
在Jinja模板中,"alpha"
和"NA"
可以由{{ stage }}
和{{ region }}
引用.
In your Jinja templates, "alpha"
and "NA"
can be referenced by {{ stage }}
and {{ region }}
.
烧瓶文档: http://flask.pocoo.org/docs/0.12/templating/#context -处理器
这篇关于Flask模板中的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!