I have a config class that has the config for my Flask app. Most of the config options are picked up, but the secret_key
remains unset, and so using the session raises an error. Why isn't the config fully imported?
app.py
:
app = Flask(__name__)
app.config.from_object('config.BaseConfig')
config.py
:
class BaseConfig(object):
DEBUG = True
SQLALCHEMY_DATABASE_URI = 'sqlite:///test.db'
SQLALCHEMY_TRACK_MODIFICATIONS = False
secret_key = '122332'
When I access a view that uses the session, I get this error:
RuntimeError: the session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret.
SECRET_KEY: Flask "secret keys" are random strings used to encrypt sensitive user data, such as passwords. Encrypting data in Flask depends on the randomness of this string, which means decrypting the same data is as simple as getting a hold of this string's value.
Flask can be instructed to load all environment variables starting with a specific prefix into the config using from_prefixed_env() . The variables can then be loaded and accessed via the config with a key equal to the environment variable name without the prefix i.e. The prefix is FLASK_ by default.
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.
Config keys are all uppercase, to distinguish between config and other attributes on whatever object is being scanned. The key secret_key
is lower case, so it's not picked up.
SECRET_KEY = 'stack overflow'
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