Last active
January 6, 2019 03:55
-
-
Save yoojinyoung/699a94faf260e6a9d0228e6201a55764 to your computer and use it in GitHub Desktop.
django settings.py code for deloyment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
from django.core.exceptions import ImproperlyConfigured | |
# Set ENV with system variable 'DJANGO_ENV'. If 'DJANGO_ENV' is not present, then use 'development' as ENV. | |
# In production server, set 'DJANGO_ENV' as 'production' | |
ENV = os.getenv('DJANGO_ENV', 'development') | |
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) | |
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
CONFIG_DIR = os.path.dirname(os.path.abspath(__file__)) | |
CONFIG_VARIABLES_DIR = os.path.join(CONFIG_DIR, 'variables') | |
# Set paths of variable files | |
CONFIG_VARIABLES_FILE = os.path.join(CONFIG_VARIABLES_DIR, ENV + '.json') | |
with open(CONFIG_VARIABLES_FILE) as file: | |
CONFIG_VARIABLES = json.loads(file.read()) | |
def get_config_variable(key, config_variable=CONFIG_VARIABLES, env=ENV): | |
try: | |
return config_variable[key] | |
except KeyError: | |
error_msg = "Set the variable '{}' in 'project/config/variables/{}.json'".format(key, env) | |
raise ImproperlyConfigured(error_msg) | |
# Below variables will be loaded from development.py or production.py. | |
# SECRET_KEY, DEBUG, ALLOWED_HOSTS | |
SECRET_KEY = get_config_variable('SECRET_KEY') | |
DEBUG = get_config_variable('DEBUG') | |
ALLOWED_HOSTS = get_config_variable('ALLOWED_HOSTS') | |
# ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment