Access config values in Flask from other files
Question:
I have the following app structure:
manage.py
myapp/
__init__.py
config.py
views/
__init__.py
login.py
...
In myapp/__init__.py
I have a function create_app()
which returns the Flask app instance. The config values are also stated in create_app()
too. I would like to be able to access these values in other files such as login.py
. I’ve tried:
from myapp import create_app as app
print app.config['SECRET_KEY']
However I receive an error stating AttributeError: 'function' object has no attribute 'config'
What am I doing wrong and how can I fix this? Thanks.
Answers:
Use from flask import current_app
. You define SECRET_KEY
in settings.py
.
print current_app.config['SECRET_KEY']
This worked for me, for the next person that needs this. I had a similar setup from the tutorials.
from myapp import create_app
app = create_app()
print app.config['SECRET_KEY']
I have the following app structure:
manage.py
myapp/
__init__.py
config.py
views/
__init__.py
login.py
...
In myapp/__init__.py
I have a function create_app()
which returns the Flask app instance. The config values are also stated in create_app()
too. I would like to be able to access these values in other files such as login.py
. I’ve tried:
from myapp import create_app as app
print app.config['SECRET_KEY']
However I receive an error stating AttributeError: 'function' object has no attribute 'config'
What am I doing wrong and how can I fix this? Thanks.
Use from flask import current_app
. You define SECRET_KEY
in settings.py
.
print current_app.config['SECRET_KEY']
This worked for me, for the next person that needs this. I had a similar setup from the tutorials.
from myapp import create_app
app = create_app()
print app.config['SECRET_KEY']