Skip to content

Instantly share code, notes, and snippets.

@georgebyte
Last active March 6, 2017 06:40
Show Gist options
  • Save georgebyte/5214483 to your computer and use it in GitHub Desktop.
Save georgebyte/5214483 to your computer and use it in GitHub Desktop.
Django: Environment variable in Django settings
# Load environment variable into Django settings.
# Usage: SOME_SECRET_KEY = get_env_variable("SOME_SECRET_KEY")
import os
from django.core.exceptions import ImproperlyConfigured
def get_env_variable(var_name):
""" Get the environment variable or return exception """
try:
return os.environ[var_name]
except KeyError:
error_msg = "Set the %s environment variable" % var_name
raise ImproperlyConfigured(error_msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment