Flask - 访问模板中的配置变量 [英] Flask - Accessing the config variable in the template

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

问题描述

我正在使用Flask版本0.7。我将静态内容的路径存储在配置文件中,并使用

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆