Skip to content

Instantly share code, notes, and snippets.

@gjbagrowski
Created January 23, 2017 13:09
Show Gist options
  • Save gjbagrowski/42fe8a630bfdfc890b6b97d6734948c2 to your computer and use it in GitHub Desktop.
Save gjbagrowski/42fe8a630bfdfc890b6b97d6734948c2 to your computer and use it in GitHub Desktop.
Django-environ example
# project-repo/app/settings/.env-example
#
# DJANGO
#
SITE_HOST=localhost:8000
USE_SSL=False
ALLOWED_HOSTS=localhost,127.0.0.1
SECRET_KEY=asdasd
DEBUG=true # never on production, will cause settings including api keys to leak
DJANGO_LOG_LEVEL=DEBUG
DJANGO_DEBUG_TOOLBAR=false
DJANGO_FILE_DIR=../dev
#
# POSTGRES
#
# DATABASE_URL=psql://<username>:<password>@127.0.0.1:8458/<database_name>
DATABASE_URL=psql://project1:[email protected]:8458/project1
#
# REDIS
#
# django cache
REDIS_DJANGO_CACHE_DATABASE=5
REDIS_DJANGO_CACHE_URL=rediscache://127.0.0.1:6379:5?client_class=django_redis.client.DefaultClient
# celery configuration
REDIS_CELERY_RESULTS_DATABASE=6
REDIS_CELERY_RESULTS_URL=redis://127.0.0.1:6379/6
CELERY_RESULT_BACKEND=redis://127.0.0.1:6379/6
CELERY_BROKER_URL=amqp://user:password@localhost:5672/project1-celery
# project-repo/app/settings/default.py
from .env import env
SITE_HOST = env.str('SITE_HOST')
SITE_URL_HTTP = 'http://{}'.format(SITE_HOST)
SITE_URL_HTTPS = 'https://{}'.format(SITE_HOST)
if env.bool('USE_SSL', default=False):
DEFAULT_SITE_URL = SITE_URL_HTTPS
else:
DEFAULT_SITE_URL = SITE_URL_HTTP
ALLOWED_HOSTS = tuple(env.list('ALLOWED_HOSTS', default=[]))
SECRET_KEY = env.str('SECRET_KEY')
# project/
REPO_ROOT_DIR = environ.Path(__file__) - 3
# project/dev/
DEV_DIR = REPO_ROOT_DIR.path('dev')
# project/dev/ or DJANGO_FILE_DIR
FILE_DIR = env.path('DJANGO_FILE_DIR', default=DEV_DIR())
# FILE_DIR is a Path object
# FILE_DIR() -> path string,
# FILE_DIR.path('db') -> Path object
if env.str('DATABASE_URL', default=''):
DATABASES = {
'default': env.db(),
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': FILE_DIR.path('db')('django.sqlite3'),
},
}
# project-repo/app/settings/env.py
import environ
from common.settings import Env
__all__ = (
'env',
)
env = Env()
# read .env file from default location or ENV_FILE_PATH
env.read_env(
env.path(
'ENV_FILE_PATH',
default=(environ.Path(__file__) - 1).path('.env')()
)())
# project-repo/deployment/requirements.txt
# http://django-environ.readthedocs.io/en/latest/
django-environ==0.4.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment