Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask doesn't get secret_key config from config object

Tags:

python

flask

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.
like image 827
Nickpick Avatar asked Apr 22 '16 15:04

Nickpick


People also ask

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

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.

How do I access my Flask app config?

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.

How do I use .ENV in Flask?

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.


1 Answers

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'
like image 111
davidism Avatar answered Sep 30 '22 16:09

davidism