![]() |
VOOZH | about |
When developing a Django application, managing settings for different environments (local development, testing, staging, and production) is crucial for ensuring smooth deployment and security. You don't want the same settings (like debug mode or database credentials) for local development and production. In this article, weβll explore different strategies to manage local vs production settings in Django effectively.
Local development environments and production environments serve different purposes:
DEBUG mode on and has relaxed security measures.Keeping the settings separate helps ensure:
DEBUG setting, which should be True in development, must always be False in production to prevent leaking sensitive information in error pages.Here are a few key settings that differ between local and production environments:
True locally but must be False in production.['localhost', '127.0.0.1'].Note: These all parameters are managed from the settings.py file.
One of the most common ways to manage local vs production settings is by creating multiple settings files.
settings directoryIn your Django project, create a settings/ directory inside the main project directory.
myproject/
βββ myproject/
βββ settings/
βββ __init__.py
βββ base.py
βββ local.py
βββ production.py
base.py)Start by creating a base.py file for settings that are common to both environments.
local.py)In the local.py file, inherit from base.py and customize settings for development.
production.py)In the production.py file, inherit from base.py and override the necessary settings for production.
Modify __init__.py: Set a default settings file (like local.py) in the __init__.py file, or load environment-specific settings dynamically.
Setting Environment Variables: In production, set the DJANGO_ENV environment variable to 'production':
export DJANGO_ENV=productionRunning the Application: Django will now load the correct settings based on your environment variable.
Another approach is to manage sensitive and environment-specific settings using environment variables.
os.environ in Your SettingsIn your settings.py, replace sensitive values with environment variables:
Set environment variables locally in your terminal:
export DJANGO_SECRET_KEY='your-secret-key'
export DJANGO_DEBUG=True
export DJANGO_ALLOWED_HOSTS='localhost,127.0.0.1'
In production, configure environment variables using your hosting provider or .env files for tools like Docker or Heroku.
django-environThe django-environ package is an excellent tool for managing environment variables in a more structured way. It allows you to store your configuration in .env files and load them into your Django settings.
Install django-environ:
pip install django-environ.env FileAdd a .env file to the root of your project:
DJANGO_SECRET_KEY=your-secret-key
DJANGO_DEBUG=True
DJANGO_ALLOWED_HOSTS=localhost,127.0.0.1
settings.pyLoad environment variables in settings.py:
Managing local vs production settings in Django is crucial for developing secure, scalable applications. Whether you choose multiple settings files, environment variables, or a combination of both (using tools like django-environ), itβs important to keep your configurations modular, secure, and easy to maintain. By separating local and production settings, you can ensure your application behaves appropriately in different environments and stays secure during deployments.