Splitting Django Settings for Local and Production Development

<project name>
├── <project name>
│ ├── __init__.py
│ └── settings
│ ├── __init__.py
│ ├── base.py
│ ├── local.py
│ └── production.py
└── .dockerignore

__init__.py file

from .base import *try:
from .local import *
live = False
except ImportError:
live = True
if live:
from .production import *

base.py file

BASE_DIR = ******
# This depends on you.
SECRET_KEY = ******
INSTALLED_APPS = [
...
]
MIDDLEWARE = [
...
]
ROOT_URLCONF = '******'
TEMPLATES = [
...
]
WSGI_APPLICATION = '******'
AUTH_PASSWORD_VALIDATORS = [
...
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
LOGGING = {
...
}
EMAIL_HOST = ******
EMAIL_PORT = ******
EMAIL_USE_SSL = ******
EMAIL_HOST_USER = ******
EMAIL_HOST_PASSWORD = ******

local.py file

DEBUG = TrueALLOWED_HOSTS = ['*']DATABASES = {
...
}
SHARE_URL = "http://127.0.0.1:8000"# Static assets
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static', 'static_root')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static', 'static_dirs'),
)
# User uploads
MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'media')

production.py file

DEBUG = FalseALLOWED_HOSTS = ['******']# Database connection to Azure URL
DATABASES = settings.DATABASES
DATABASES['default'] = dj_database_url.parse('******', conn_max_age=500,
ssl_require=True)
# For django storages
AZURE_ACCOUNT_NAME = ******
AZURE_ACCOUNT_KEY = ******
AZURE_CONTAINER = ******
SHARE_URL = "******"AZURE_BLOB_CUSTOM_DOMAIN = '******'STATICFILES_LOCATION = 'static'
STATICFILES_STORAGE = '******'
STATIC_URL = "https://%s/%s/" % (AZURE_BLOB_CUSTOM_DOMAIN, STATICFILES_LOCATION)
MEDIAFILES_LOCATION = 'media'
DEFAULT_FILE_STORAGE = '******'
MEDIA_URL = "https://%s/%s/" % (AZURE_BLOB_CUSTOM_DOMAIN, MEDIAFILES_LOCATION)

Finally

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akshay Gollahalli

Akshay Gollahalli

PhD student, currently doing research on Spiking Neural Networks and Brain Computer Interfaces