Skip to content

Instantly share code, notes, and snippets.

@yoojinyoung
Last active January 6, 2019 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoojinyoung/699a94faf260e6a9d0228e6201a55764 to your computer and use it in GitHub Desktop.
Save yoojinyoung/699a94faf260e6a9d0228e6201a55764 to your computer and use it in GitHub Desktop.
django settings.py code for deloyment
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