How to manage Local vs Dev vs Prod settings.py in latest Django RestAPI?

github logo ・1 min read

Django Best Practices

As we have some common settings in both Local, Dev, and Prod settings, and also have settings that are different from Local, Dev, and Prod; such as database connections, installed apps, etc...I would like to start my Django app with custom settings_.py file for each environment so that we can have better visibilty in settings and also will not cause git conflicts....

twitter logo DISCUSS (8)
markdown guide
 

I used to run a small website for my campus's library employees ran with Django. So take this with a grain of salt.

We would keep all common settings in a settings.py and then have a nonprod_settings.py and prod_settings.py. You can then point to the environment specific settings.py with:

export DJANGO_SETTINGS_MODULE=<nonprod/prod>_settings.settings.

For common settings, at the bottom of our environmental settings we would from common_settings import *.

Info here: https://docs.djangoproject.com/en/2.1/topics/settings/.

 

I'd take a look at this article for how to setup multiple settings modules. However, I do use environment variables (DJANGO_SETTINGS_MODULE) to load the correct one in CI and in Docker containers.

 
 

Thanks I will test out this link and update on how it goes..

 

Not an avid django/python user but maybe consider keeping local, dev and prod in .env files and passing the variables into the settings.py file. That way any sensitive info is kept consistently in key value pair files and can be easily swapped. Might be inaccurately prescribed, it comes from node.js tinkering. Best of luck. 😁

 

We are using os.getenv method and working with .env files to set the enviroment data, works pretty well.

 

this article worked for me. If anyone is still looking for a solution...

Classic DEV Post from Dec 31 '18

Who's looking for open source contributors? (Dec 31st edition)

Find something to work on or promote your project here. Please shamelessly pro...

Suri Nuthalapati profile image
Data has a Better Idea