How to define settings file for local, development and production environment in Django?

Published on May 18,2020 by Maulik

Automating the right configurations for local and production settings for the Django web application can save time before any deployment. By using different configurations, files save time for developers as well to set up a local environment in the local machine. We can restrict the access of production credentials to the limited developer or only Devops member by keeping credentials in environment variables.

What is the settings file in Django?

The settings.py file has all configurations of the Django web application. For example, how many applications are installed, what time zone server will consider, database connectivity configurations, static files configurations, whether debug mode is on or off?

Here is the official explanation of settings module from Django documentation.

If we have one single settings.py, we have to update the debug mode, database settings, and global variables used in production differently. So it is best practice to have multiple settings file for multiple environments.

It is best practice to store secrets in environment variables and use them in settings.py. I have written a blog on how to manage secrets and sensitive information for Django web application development.

Steps to define local and production settings file in Django web application development

  1. Create separate settings files for the local and production environment.
  2. Load the environment variables in settings files.
  3. Run the server

To know what is environment variables and how to use the environment variables to maintain secrets, please check How to use env variables to hide passwords/secrets from env files?

Please check the GitHub repository for the project source code.

Create separate settings files for the local and production environment.

  • When you create a Django application, it will create settings.py file with default settings configuration. But always, the settings configurations are different for local machines and production servers. We must manage different settings files for a different environment. 
  • Create settings package and create multiple settings files as below:
  • .
    ├── manage.py    
    ├── requirements.txt  
    ├── django_settings_configurtaion 
        │   ├── settings
        │   │   ├── __init__.py
        │   │   ├── default.py
        │   │   ├── local.py
        │   │   ├── staging.py
        │   │   ├── production.py
        │   ├── urls.py
        │   ├── wsgi.py
  • The purpose of the default.py  is to contain all common configurations ( like “INSTALLED_APPS,” “MIDDLEWARE,” and other configurations ) in it.
  • The purpose of local.py or stagging.py or production.pyis to contain all environment-specific configurations like “like “DATABASES” and other configurations.
  • So now, default.py file contains the common configuration for all the environments, and other files ( local.py, staging.py, and production.py ) contain the different environment-specific configurations.

Load the settings configurations on the basis of the environment.

  • Install python-dotenv library by pip install python-dotenv package.
  • This library loads .env file variables to operating system environment variables.
  •  create “.env” file inside the settings file and update the files as follow:
  • ├── settings/.env        
APP_ENVIRONMENT="local"
  • Edit __init__.py file as below:
  • ├── settings/__init__.py
  • import os
    from dotenv import load_dotenv
    load_dotenv()
     
    ENVIRONMENT = os.getenv("ENVIRONMENT", os.getenv("APP_ENVIRONMENT"))
     
    if ENVIRONMENT == "staging":
       from .staging import *
    elif ENVIRONMENT == "production":
       from .production import *
    else:
       from .local import *

     

  • So now, when the settings module will be interpreted by Django dynamically, the value of the environment variable “APP_ENVIRONMENT” defines to load which settings module in the Django application server.

Run the server

  • Now, as we have mentioned APP_ENVIRONMENT= “local” the local settings configurations will be loaded.
  • To run the server use the command python manage.py runserver

Conclusion

  • We are now loading different settings modules dynamically.
  • By adjusting the environment variable, the Appropriate settings module gets loaded on the Django web application server. 
  • So you can get rid of commenting out local development settings from settings file in Django web application before deploying to google cloud, aws, azure, or any virtual machine.
  • I am sure this practice will save you time and avoid mistakes of deploying in production with local configurations that are avoided.

0 Comments

Related Articles

How to store Django logs in MongoDB database?

Published on May 18,2020 by Maulik

How to store Django logs in MongoDB database?

Info, Warning, or Error logs are vital information that helps to detect issues and solving them. When developing our support tools for our product, we …

Read full article

Debugging a Django web application in VS Code

Published on May 12,2020 by Maulik

Debugging a Django web application in VS Code

Web applications, when built in their entirety, are a complex piece of software passing the execution of the code from one file to another within …

Read full article

How to create a common response format for 200, 400, 500 responses by creating custom exception handler in Django Rest Framework?

Published on May 28,2020 by Maulik

How to create a common response format for 200, 400, 500 responses by creating custom exception handler in Django Rest Framework?

In micro-services architecture, multiple client applications are consuming the backend API. The backend server does the core business logic and all heavy lifting. The client …

Read full article

copied to clipboard

Sign up for our newsletter

Please join our news letter which we share every month, you would love interesting python and django news letters.

We understand no one like spamming, your emails are safe with us.

Copyright © Django Circle All Rights Reserved.