Splitting Django Settings for Local and Production Development

I have been working on a few projects that use Django 2+ and deploying them to Azure Docker container. I do have one problem though; my development settings are very much different from my production. So, I have divided settings.py into a package with — base.py, local.py and production.py:

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

__init__.py file

Here, we check for local.py, if it exists this is imported and if not, production.py is used.

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

base.py file

This is the base for both local and production, the common code. Now it depends if you want to use the same SECRET_KEY on both production and local, I use it in both ways. If it’s a commercial project, I separate it and if not I put it in the base.py. Also, if you are using external logging providers such as Logstash or Sentry, you might want to separate LOGGING too.

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

I always separate my database, and I also make sure that they are the same type of database, in my case it’s PostgreSQL. Also, it contains the location for the static file and where the user can upload their files.

Code:

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

This is where I use external services such as Azure, AWS or Heroku to add their production settings. I also restrict the access to one domain, disable DEBUG, use whitenoise for static assets and use dj_database_url for parsing PostgreSQL URL.

The code:

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

One last tip is that I use my secrets as an environment variable or use the Azure Key Vault; this gives extra security and flexibility.

Happy coding.

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What are Daily Scrum events?

Driving an open-source community using productboard

Working with ParseHub, BoW, and Td-IDF

How to Add Real-Time Messaging On A Codeigniter App in Under 5 Minutes!

Apriori: Association Rule Mining In-depth Explanation and Python Implementation

Dappio Newsletter #3

Setting up a multi node private ethereum swarm network without a blockchain

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

More from Medium

Introduction to Django Framework

introduction to Django Framework

[Django] Django development notes (2) Templates

Setting up Django in Windows