Explain Codes LogoExplain Codes Logo

How to manage local vs production settings in Django?

python
best-practices
tools
environment-variables
Nikita BarsukovbyNikita Barsukov·Feb 19, 2025
TLDR

Leverage a modular settings strategy in Django by organizing your settings into base, local, and production files inside a settings package. This elegant method provides a way to inherit from the base settings, offering a seamless way to differentiate configurations by specifying the DJANGO_SETTINGS_MODULE environment variable.

Quick Setup:

# settings/base.py # All the generic stuff # settings/local.py from .base import * DEBUG = True # Because we need all the details when things go wrong, right? # settings/production.py from .base import * DEBUG = False # Let's not scare the users with scary error messages, shall we?

To turn on a specific setting file:

# Local environment export DJANGO_SETTINGS_MODULE=mysite.settings.local # Production environment export DJANGO_SETTINGS_MODULE=mysite.settings.production

Being mindful of environment-specific elements like DEBUG is crucial for a secure and effective configuration following Django's best practices.

Arranging your settings files

The Art of Organizing Settings

Django doesn't force a specific structuring of settings. Be smart:

  • Common settings: Keep them in base.py to avoid repetition.
  • Specific settings: Override or extend the base settings in local.py and production.py.

Handling Sensitive Data

  • Always keep secrets and credentials as environment variables, or adopt packages like django-environ or python-decouple to assist you.
  • Always ensure that local_settings.py isn't committed by including it in .gitignore.

Dynamic Settings Adaptation

  • Leverage environment variables or Django’s ability to select settings dynamically.
  • Consider implementing a production hostnames list to accurately infer the correct settings file.

Structuring Settings

An effective layout might look like the one below. Easy to navigate, isn't it?

mysite/ |- settings/ | |- __init__.py | |- base.py | |- local.py | |- production.py | |- local_settings.py (optional, untracked)

Use logic in __init__.py to automatically select the right settings file based on the environment or hostname.

Practical tips, tricks, and best practices

Best Practices Make Best Applications

  • Fail fast and loud: Let the system raise an error if an essential environment variable isn't set.
  • Version control: Track changes without exposing sensitive data.
  • Leverage Django's toolkit: The --settings flag and manage.py commands are your friends.

Addressing Common Pitfalls

Some potential issues, and how to overcome them:

  • Misconfiguration: Ensure settings files are correctly named and located.
  • Environment mismatches: Manage environment variables carefully to maintain consistency.
  • Secret leaks: Regular audits to ensure no sensitive data slips into the repo.

Beyond Only Local and Production

You might need test, staging, or CI environments. Create additional settings files for these, following the same principles.