Flask - Accessing the config variable in the template

PythonFlaskJinja2

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionranendraView Question on Stackoverflow
Solution 1 - PythonmdeousView Answer on Stackoverflow