[Fixed]-Getting raise KeyError(key) KeyError: 'SECRET_KEY' with django on production settings

13👍

I think you are trying this locally, and don’t have the SECRET_KEY setup in your environment.

Set it using

export SECRET_KEY="somesecretvalue"

and then running python manage.py shell --settings=entri.settings.prod should work fine.

2👍

I use os.getenv(‘SECRET_KEY’), instead of os.environ[‘SECRET_KEY’]

print os.getenv('SECRET_KEY')    #returns None if KEY doesn't exist
print os.getenv('SECRET_KEY', 0) #will return 0 if KEY doesn't exist 

my python version is 2.7.12

👤chank

0👍

In Django while trying to secure/hide my secret_key, my problem was even after setting the secret_key using the set command on windows, I still got a ‘Key must not be empty’ error. I solved that by removing all the spaces before and after the assignment operator in the command. In your cmd, write

set SECRET_KEY="kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk"

instead of

set SECRET_KEY = "kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk"

Leave a comment