Global variables in Flask templates

后端 未结 1 550
闹比i
闹比i 2021-01-04 06:36

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 the

相关标签:
1条回答
  • 2021-01-04 06:48

    I assume your Flaskobject's name is app (i.e., app = Flask(__name__)).

    Place the below code right after the app is initialized.

    @app.context_processor
    def inject_stage_and_region():
        return dict(stage="alpha", region="NA")
    

    In your Jinja templates, "alpha"and "NA" can be referenced by {{ stage }} and {{ region }}.

    Flask docs: http://flask.pocoo.org/docs/0.12/templating/#context-processors

    To inject new variables automatically into the context of a template, context processors exist in Flask. Context processors run before the template is rendered and have the ability to inject new values into the template context. A context processor is a function that returns a dictionary. The keys and values of this dictionary are then merged with the template context, for all templates in the app

    0 讨论(0)
提交回复
热议问题