nickpick nickpick - 7 months ago 10
Python Question

Flask doesn't get secret_key config from config object

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.

Answer

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'
Comments