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.
To import from config file in Python Flask, we can use the config. from_object method. to create config classes in the config.py file. in app.py to load the config from the ProductionConfig class in the config.py file with the app.
env file inside the Python Flask application. Reads the key-value pair from the . env file and adds them to the environment variable. It is great for managing app settings during development and in production using 12-factor principles.
In order to use session in flask you need to set the secret key in your application settings. secret key is a random key used to encrypt your cookies and save send them to the browser. To fix this you just need to set a SECRET_KEY in your config file.
Use from flask import current_app
. You define SECRET_KEY
in settings.py
.
print current_app.config['SECRET_KEY']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With