How to Manage Local vs Production Settings in Django

Django, a versatile web framework for Python, allows you to create web applications for different environments, such as development, testing and production. To ensure your application runs smoothly in these distinct settings, its crucial to manage local and production settings efficiently. In this blog post, we explore best practices for handling settings in Django for various environments.


Django applications typically rely on the file to configure various aspects of the application. This file includes settings related to databases, authentication, security and more. To manage settings for different environments, consider the following approaches:

Environment Variables

Use environment variables to store sensitive information or settings that vary between environments, such as API keys and database credentials. The python-decouple library is a useful tool for managing environment variables in Django. It allows you to define configuration settings in .env file and access them easily in your settings file.

  1. Install python-decouple:
pip install python-decouple
  1. Create a .env file in your project’s root directory and define environment-specific variables:
# .env
  1. In your, use config from decouple to read the environment variables:
from decouple import config

DEBUG = config('DEBUG', default=False, cast=bool)

By using environment variables you can easily switch configurations between environments without modifying your code.

Configuration Files

Another approach is to create separate configuration files for different environments, such as, and In each file, define environment-specific settings and then use a environment variable to specify which configuration to use.

  1. Create separate configuration files, e.g.,,, etc., for each environment.
  2. In your or file, use the DJANGO_SETTINGS_MODULE environment variable to set the appropriate settings module based on the environment.
import os

from django.core.wsgi import get_wsgi_application

env = os.getenv('DJANGO_ENV', 'development')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', f'your_project.settings_{env}')

application = get_wsgi_application()

By specifying the DJANGO_ENV environment variable, you can choose the appropriate settings module.

Third-Party Packages

There are third-party packages like django-environ and python-decouple that provide more advanced configuration options, including handling multiple settings files, better support for complex data types and secure handling of secrets.

  1. Install django-environ:
pip install django-environ
  1. Use it to load environment variables and configuration settings:
import environ

env = environ.Env()

DEBUG = env('DEBUG', default=False)

Configuration Management

It crucial to establish a system for maintaining configuration files and environment variables. Here are some best practices:

  1. Use Version Control: Include environment-specific configuration files in your version control system, but exclude sensitive data like secret keys.
  2. Keep Secrets Secure: Store secret keys and other sensitive information securely. Utilize environment-specific secrets files or services like AWS Secrets Manager, HashiCorp Vault, or similar tools.
  3. Document Configurations: Document your configuration settings and provide clear instructions for team members on how to set up and manage different environments.
  4. Automate Deployment: Use deployment automation tools like Docker, Kubernetes or platform-specific solutions to ensure consistent environment configurations.


Effectively managing local and production settings in Django is essential for building reliable web applications. By following best practices and utilizing tools like decouple, environ and configuration files, you can easily switch between environments and maintain secure consistent settings throughout your development and deployment process.