wonderbeyond
2/21/2017 - 6:05 AM

Separate django settings for multiple environments

Separate django settings for multiple environments

Separate django settings for multiple environments

See also: https://www.coderedcorp.com/blog/django-settings-for-multiple-environments/


Make settings a package

structure

myproject/
    env.py             -- detect the environment
    settings/
        __init__.py    -- settings entry(load specific settings based on env)
        base.py        -- shared common settings
        dev.py         -- development-only settings
        prod.py        -- production-only settings

myproject/env.py

import os

# Default to dev for safety.
APP_ENV = os.environ.get('APP_ENV', 'dev')

settings/__init__.py

from myproject.env import APP_ENV

if APP_ENV in ('dev', 'prod'):
    exec('from .%s import *' % APP_ENV)

settings/base.py

# ...
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(
    os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
)
# ...

settings/dev.py

from .base import *
DEBUG = True
# ...

settings/prod.py

from .base import *
DEBUG = False
# ...

Specify env vairable at runtime

APP_ENV=dev python manage.py runserver