DEV Community

loading...

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

thammuio profile image Suri Nuthalapati ・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....

Discussion (8)

pic
Editor guide
Collapse
seancallaway profile image
Sean Callaway

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.

Collapse
thammuio profile image
Suri Nuthalapati Author

this article Worked for me. Thank you..

Collapse
seancallaway profile image
Sean Callaway

Glad to hear it!

Collapse
thammuio profile image
Suri Nuthalapati Author

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

Collapse
cbrintnall profile image
Christian Brintnall

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/.

Collapse
jacksonelfers profile image
Jackson Elfers

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. 😁

Collapse
haruanm profile image
Haruan Justino

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

Collapse
thammuio profile image
Suri Nuthalapati Author

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