DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Scaling Microservices With Docker and Kubernetes on Production
  • Debugging With Confidence in the Age of Observability-First Systems
  • From Zero to Production: Best Practices for Scaling LLMs in the Enterprise
  • The Production-Ready Kubernetes Service Checklist

Trending

  • The Ultimate Guide to Code Formatting: Prettier vs ESLint vs Biome
  • Proactive Security in Distributed Systems: A Developer’s Approach
  • How to Format Articles for DZone
  • Building a Real-Time Change Data Capture Pipeline With Debezium, Kafka, and PostgreSQL
  1. DZone
  2. Coding
  3. Frameworks
  4. Django Settings for Production and Development: Best Practices

Django Settings for Production and Development: Best Practices

By 
Tommy Jarnac user avatar
Tommy Jarnac
·
Mar. 07, 12 · Interview
Likes (0)
Comment
Save
Tweet
Share
43.8K Views

Join the DZone community and get the full member experience.

Join For Free

When you’re just starting out with Django, it can be overwhelming to see there’s no standard approach to deal with settings. However, there are a few simple best practices that work when you start needing more than the basic settings file.

What’s the problem? Django stores all the settings in a project-wide settings.py file. All is fine until the moment you need different settings for different environments(such as a production environment and a development environment to start with).

Of the necessity to have different settings files

Your development settings should be different from your production settings. Why?

  • you should protect sensitive things like database passwords, api secrets, private keys in a separate file
  • the behavior of production code is not suited for development : for example you don’t want to send an email when you’re developing new features


Here are a few things that may change from one environment to another:

  • database details: database name, user name and password
  • api keys
  • your own private keys for encrypting data
  • debug variables (DEBUG and TEMPLATE_DEBUG)
  • path to different tools needed by your Django applications
  • and any other flags needed to make your application work differently in development and in production


We’re going to compare three different ways to organize your settings when having one setting file doesn’t cut it anymore.

First solution: local settings

This solution relies on having one settings.py file with common settings and a local_settings file where you define environment-specific settings.

Let’s see:

### settings.py file
### settings that are not environment dependent
try:
    from local_settings import *
except ImportError:
    pass
### local_settings.py
### environment-specific settings
### example with a development environment
DEBUG = True
DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'django',
    'USER': 'django',
    'PASSWORD': '1234',
    'HOST': '',
    'PORT': '',
}

  • Advantages: simple if you only need a development and a production environment (no staging) – the local_settings.py should stay out of source control and you need to have a separate one for development and production.
  • Disadvantages: it limits what you can do with settings such as modify common settings in the local_settings for example. It can work for the most simple case though.

Second solution: environment-based settings

This is one from Ches Martin. It relies on having an environment variable pointing to the right python module. It has the advantage of being explicit, so you can name your specific settings files explicity (production file being production.py for example).

[myapp]$ls settings
__init__.py
defaults.py
dev.py
staging.py
production.py


What happens when we do import settings? Settings is in this case a package (a package in Python being a directory with an __init__.py file inside), so when the interpreter loads the package it executes __init__.py.

By default, let’s make it work in development environment.

### __init.py__
from dev import *
### default.py__
### sensible choices for default settings
### dev.py
from defaults import *
DEBUG = True
### other development-specific stuff
### production.py
from defaults import *
DEBUG = False
### other production-specific stuff

How to use it in production and staging environments?

The trick is that the settings module location can be overriden by settings an environment Django variable. So we need to override that variable before starting our webserver. For example if you use Apache as your Django web server, modify your Apache configuration file with:

SetEnv DJANGO_SETTINGS_MODULE myapp.settings.production

Third solution: system-wide settings

### settings.py
import os
ENVIRONMENT_SETTING_FILE = '/etc/django.myproject.settings'
### this will load all environment file settings in here
execfile(ENVIRONMENT_SETTING_FILE)
### all common settings
### ...

We can see one problem of doing it this way is that we cannot modify variables defined in the common settings.py file in the environment-specific files.

On the other hand, it simplifies the management of environment-specific settings files. Create a file for development, staging and production, secure it with some tight permissions, and forget about it.

To conclude

There are other ways to manage your settings, so you can experiment a little bit. Check the list of resources to know more about managing your settings. And drop a comment here if you want to share the way you’re doing it!

Resources:

  • Splitting up the settings file, very comprehensive resource from the official Django website
  • Django settings, again from the official Django website
  • Extending Django settings for the real world from Yipit
  • Django settings at Disqus: for a more complex and modular settings configutation.
  • Stack Overflow:
    • How to modularize Django settings.py?
    • How do you configure Django for simple development and deployment?
  • Django Snippets Keep settings.py in version control safely

Django (web framework) Production (computer science)

Published at DZone with permission of Tommy Jarnac. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Scaling Microservices With Docker and Kubernetes on Production
  • Debugging With Confidence in the Age of Observability-First Systems
  • From Zero to Production: Best Practices for Scaling LLMs in the Enterprise
  • The Production-Ready Kubernetes Service Checklist

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!