Dulangi_Kanchana
Dulangi_Kanchana

Reputation: 1233

How to get secret key in process of connecting flask app to database

I m following a tutorial on making a database connection between Flask and PostgreSQL using json, and there is a secret key, mentioned in config.py

I have read some other answers and understood Flask uses urandom to generate a random key. But not exactly clear at which moment of time i must run this code to generate the secret key. I do understand that this code must be run on command prompt.

python
    >>> import os
    >>> os.urandom(24)

My config.py code

import os
basedir = os.path.abspath(os.path.dirname(__file__))

class Config(object):
    DEBUG = False
    TESTING = False
    CSRF_ENABLED = True
    SECRET_KEY = 'this-really-needs-to-be-changed'
    SQLALCHEMY_DATABASE_URI = os.environ['postgresql://postgresql:silverTip@localhost/DatabaseFirst']

class ProductionConfig(Config):
    DEBUG = False

class StagingConfig(Config):
    DEVELOPMENT = True
    DEBUG = True

class DevelopmentConfig(Config):
    DEVELOPMENT = True
    DEBUG = True

class TestingConfig(Config):
    TESTING = True

Upvotes: 1

Views: 1169

Answers (1)

Lord Elrond
Lord Elrond

Reputation: 16002

Run that code in a python shell:

>>> import os
>>> os.urandom(24)
b'\x1d\xc6\x0f[\xed\x18\xd6:5\xe0\x0f\rG\xaf\xb4\xf4HT\xef\xc1\xf1\xa89f'

Then copy/paste the result into your config file:

SECRET_KEY = '\x1d\xc6\x0f[\xed\x18\xd6:5\xe0\x0f\rG\xaf\xb4\xf4HT\xef\xc1\xf1\xa89f'

Remember to remove the leading b, otherwise you will saving the SECRET_KEY as a bytes object, rather than a string.

Upvotes: 1

Related Questions