getting raise KeyError(key) KeyError: 'SECRET_KEY' with django on production settings

◇◆丶佛笑我妖孽 提交于 2020-12-29 09:47:05

问题


I've 2 separate settings files for production and development and a common base.py settings file
base.py

SECRET_KEY = r"!@#$%^&123456"

prod.py

from .base import *
SECRET_KEY = os.environ['SECRET_KEY']

manage.py

#!/usr/bin/env python
import os

import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings.dev")

from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)

When I enter this in terminal:

python manage.py shell --settings=entri.settings.prod

I get error:

raise KeyError(key)
KeyError: 'SECRET_KEY'

Help me, I'm new to django and python


回答1:


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



来源:https://stackoverflow.com/questions/28090391/getting-raise-keyerrorkey-keyerror-secret-key-with-django-on-production-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!