Flask - 访问模板中的配置变量 [英] Flask - Accessing the config variable in the template
问题描述
app.config.from_envvar(< file_name> )
我能否在模板中访问这个配置变量而不通过视图传递变量?
有几个全局变量默认通过flask(这里是完整的列表),其中之一是 config
,它允许您从模板访问应用程序配置。作为字典,可以使用 config ['MY_CONFIGURATION']
或 config.MY_CONFIGURATION
而另一方面,如果你想传递任意的数据到你的模板,而不必在每个视图中明确地传递它,您将不得不使用上下文处理器。
I am using Flask version 0.7. I have stored the path of static content in a configuration file and loaded it using
app.config.from_envvar(<file_name>)
Can I be able to access this config variable in the template without passing the variables through the view?
There are a few global variables that are passed in the templates context by default by flask (here is the complete list), one of them being config
, which allows you to access the application configuration from templates. Being a dictionary, it can be accessed using the syntax config['MY_CONFIGURATION']
or config.MY_CONFIGURATION
(this syntax for accessing dict items is specific to Jinja).
On the other hand, if you wanted to pass arbitrary data to your templates without having to pass it explicitely in each view, you would have to use context processors.
这篇关于Flask - 访问模板中的配置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!