본문으로 바로가기

django SECRET_KEY 따로 관리하기

category Python 2019. 12. 20. 18:20

json 파일 만들기

/common/secret_key.json 

 

secret_key.json에 작성

{
"SECRET_KEY": "키 문자열"
}

 

 

settings.py

import os, json
from django.core.exceptions import ImproperlyConfigured


secret_file = os.path.join(BASE_DIR, 'common/secret_key.json')

with open(secret_file) as f:
    secrets = json.loads(f.read())

def get_secret(setting, secrets=secrets):
    """KEY를 가져오거나 예외를 반환"""
    try:
        return secrets[setting]
    except KeyError:
        error_msg = "Set the {} environment variable".format(setting)
        raise ImproperlyConfigured(error_msg)

SECRET_KEY = get_secret("SECRET_KEY")

'Python' 카테고리의 다른 글

Confluence python-api 참고  (0) 2020.02.24
BeautifulSoup 사용  (0) 2020.02.24
django logging 설정  (0) 2019.12.16
django admin 패스워드 변경 + 제목(header) 변경  (0) 2019.12.11
(python) shotgun 참고  (0) 2019.12.05