本文介绍了Flask模板中的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我是flask/python的新手,可能不是准确的标题.我正在开发一种内部工具,供不同团队使用.每个团队都有不同的部署阶段,例如alphabeta|testprod,还具有多个区域,例如NAEUAP等...

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时,我将发送stageregion作为变量,然后在模板中使用它们.但是,对每个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 Flaskobject'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模板中的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 13:20