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.

40 Comments

DouglasMaH

7 hours, 26 minutes

sildenafil 100 mg lowest price <a href=" https://viagrasln.com/# ">sildenafil citrate 100mg for sale</a> viagra

DouglasMaH

23 hours, 19 minutes

sildenafil 20 mg <a href=" https://viagrasln.com/# ">sildenafil</a> price for viagra

DouglasMaH

1 day, 14 hours

where to buy cheap clomid online <a href=" https://clomidclo.com/# ">buy clomid</a> where to buy cheap clomid online

DouglasMaH

2 days, 6 hours

clomid <a href=" https://clomidclo.com/# ">clomid for sale canada</a> clomid tablets

Stromclall

3 days, 5 hours

pharmacy from india <a href=" https://indiadrs.online/# ">cheap generic drugs from india</a> india pharmacies online

Stromclall

3 days, 21 hours

prescriptions from india <a href=" https://indiadrs.com/# ">buy prescriptions from india pharmacy</a> india pharmacy mail order

Stromclall

4 days, 12 hours

best pills for ed <a href=" https://edpilldrs.online/# ">ed pills gnc</a> ed pills comparison

Stromclall

5 days, 4 hours

reputable canadian mail order pharmacies <a href=" https://canadadrs.online/# ">canadian pharmaceuticals online safe</a> online pharmacies without prior prescription

GregoryBUP

5 days, 23 hours

ed prescription drugs <a href=" https://canadapillsshop.com/# ">canadian drug pharmacy</a> how can i order prescription drugs without a doctor

GregoryBUP

6 days, 15 hours

how does ivermectin kill scabies <a href=" https://stromectolca.com/# ">ivermectin for dogs dosage chart</a> ivermectin for heartworms

GregoryBUP

1 week

best ed medication <a href=" https://edpillcanada.com/# ">top rated ed pills</a> ed medication online

GregoryBUP

1 week, 1 day

ivermectin tapeworm <a href=" https://stromectolca.com/# ">ivermectin malaria</a> ivermectin oral 0 8

GregoryBUP

1 week, 2 days

drug finasteride <a href=" https://finasteridemen.com/# ">finasteride price</a> propecia prices

JamesoMigh

1 week, 5 days

prednisone without prescription medication <a href=" https://prednisonemrt.com/# ">buy prednisone online</a> buy cheap prednisone

JamesoMigh

1 week, 6 days

what is the best ed pill <a href=" https://cheapdrugsmrt.online/# ">male dysfunction</a> best ed solution

JamesoMigh

1 week, 6 days

prednisone rx coupon <a href=" https://prednisonemrt.online/# ">order prednisone</a> where can i buy prednisone without prescription

JamesoMigh

2 weeks

ivermectin buy nz <a href=" https://stromectolmrt.com/# ">petco ivermectin</a> ivermectin 0.1

JamesoMigh

2 weeks, 1 day

how much ivermectin to give a dog for worms <a href=" https://stromectolmrt.online/# ">ivermectin prices</a> ivermectin how long does it take to work

Charleswar

2 weeks, 4 days

<a href=" https://stromectol.company/# ">ivermectin 6</a> generic ivermectin cream

Stacyliz

2 weeks, 5 days

cialis with dapoxetine 80mg <a href=" http://cialistadalafil.store/# ">cialis ontario no prescription</a> cost of cialis vs viagra

Stacyliz

2 weeks, 6 days

top web source for cialis <a href=" http://tadalafil.icu/# ">cialis dosage vs viagra</a> п»їgeneric cialis

Stacyliz

3 weeks, 1 day

ivermectin coronavirus <a href=" http://stromectol.icu/# ">ivermectin 200mg</a> ivermectin 12 mg

Stacyliz

3 weeks, 2 days

prednisone 50 mg tablet cost <a href=" http://deltasone.site/# ">prednisone 100 mg</a> prednisone brand name us

HaroldArbib

3 weeks, 4 days

how to overcome ed naturally <a href=" https://drugsus.shop/# ">cheap medications</a> erectal disfunction

HaroldArbib

3 weeks, 6 days

how to fix ed <a href=" http://drugsfast.store/# ">impotance</a> discount prescription drugs

HaroldArbib

4 weeks

viagra without a prescription <a href=" http://drugsen.site/# ">ed symptoms</a> help with ed

HaroldArbib

4 weeks, 1 day

homeopathic remedies for ed <a href=" https://drugsus.shop/# ">prescription drugs without prior prescription</a> ed meds online without doctor prescription

FrankNurse

1 month

875 mg amoxicillin cost <a href=" https://amoxilmst.com/# ">order amoxicillin online uk</a> how to buy amoxycillin

FrankNurse

1 month

over the counter amoxicillin <a href=" https://amoxilmst.com/# ">generic for amoxicillin</a> amoxicillin without a doctors prescription

FrankNurse

1 month

doxycycline 100mg price australia <a href=" https://doxycyclineen.shop/# ">order doxycycline</a> cheapest 40 mg doxycycline

FrankNurse

1 month

where can i buy doxycycline no prescription <a href=" https://doxycyclineen.shop/# ">doxycycline 100 mg pill</a> doxycycline 500mg capsules

FrankNurse

1 month, 1 week

prednisone cost 10mg <a href=" http://prednisonefast.site/# ">buy prednisone no prescription</a> prednisone 50

FrankNurse

1 month, 1 week

order amoxicillin online uk <a href=" https://amoxilus.fun/# ">amoxicillin 500mg prescription</a> how much is amoxicillin

EdwardAbimb

1 month, 1 week

eli lilly cialis <a href=" http://cialiscnd.com/# ">cialis and viagra together</a> order generic cialis online 20 mg 20 pills

EdwardAbimb

1 month, 1 week

cialis manufacturer coupon 2018 <a href=" http://cialiscnd.com/# ">generic cialisw</a> viagra cialis combo

Strojoism

1 month, 2 weeks

ivermectin gold <a href=" https://stromectolns.com/# ">ivermectin pour on for cattle label</a> where to buy ivermectin for dogs

Artrcriff

1 month, 2 weeks

does cialis lose effectiveness over time <a href=" https://cls20.com/# ">where to buy cialis in australia</a> cialis canada pharmacy no prescription required

Travismindy

1 month, 3 weeks

how long does 25mg viagra last <a href=" https://edviagralove.com/# ">viagra online pharmacy</a> does viagra raise your blood pressure

AbrtNew

2 months, 1 week

<a href="https://t.me/filmfilmfilmes/10">Капитан Филлипс</a>

AbertNew

2 months, 2 weeks

коли закінчиться війна в україні <a href="http://bitly.com/skilky-shche-bude-tryvaty-viyna-v-ukrayini">коли закінчиться війна в україні 2022 передбачення</a> скільки може тривати війна в україні

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.