Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask - Accessing the config variable in the template

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?

like image 472
ranendra Avatar asked Aug 18 '11 08:08

ranendra


People also ask

How do I access my Flask app config?

Use flask. current_app in place of app in the blueprint view. The current_app proxy is only available in the context of a request.

What is app config [' SECRET_KEY '] in Flask?

secret key is a random key used to encrypt your cookies and save send them to the browser. This error is because of this line in the Flask-Debugtoolbar code. To fix this you just need to set a SECRET_KEY in your config file. app.config['SECRET_KEY'] = "Your_secret_string"


1 Answers

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.

like image 177
mdeous Avatar answered Sep 23 '22 02:09

mdeous