Django: How to manage development and production settings?
The DJANGO_SETTINGS_MODULE
environment variable controls which settings file Django will load.
You therefore create separate configuration files for your respective environments (note that they can of course both import *
from a separate, "shared settings" file), and use DJANGO_SETTINGS_MODULE
to control which one to use.
Here's how:
As noted in the Django documentation:
So, let's assume you createdThe value of DJANGO_SETTINGS_MODULE should be in Python path syntax, e.g. mysite.settings. Note that the settings module should be on the Python import search path.
myapp/production_settings.py
and myapp/test_settings.py
in your source repository.In that case, you'd respectively set DJANGO_SETTINGS_MODULE=myapp.production_settings
to use the former and DJANGO_SETTINGS_MODULE=myapp.test_settings
to use the latter.
From here on out, the problem boils down to setting the
DJANGO_SETTINGS_MODULE
environment variable. Setting DJANGO_SETTINGS_MODULE
using a script or a shell
You can then use a bootstrap script or a process manager to load the correct settings (by setting the environment), or just run it from your shell before starting Django: export DJANGO_SETTINGS_MODULE=myapp.production_settings
.
Note that you can run this export at any time from a shell — it does not need to live in your .bashrc
or anything.
Setting DJANGO_SETTINGS_MODULE
using a Process Manager
If you're not fond of writing a bootstrap script that sets the environment (and there are very good reasons to feel that way!), I would recommend using a process manager:
- Supervisor lets you pass environment variables to managed processes using a program's
environment
configuration key. - Honcho (a pure-Python equivalent of Ruby's Foreman) lets you define environment variables in an "environment" (
.env
) file.
Finally, note that you can take advantage of the
PYTHONPATH
variable to store the settings in a completely different location (e.g. on a production server, storing them in /etc/
). This allows for separating configuration from application files. You may or may not want that, it depends on how your app is structured. Django project settings for production and development
When I did git pull, __init__.py
inside the settings was also changed. I did not know about it. The developer who I know advised me to check this file. I discovered that different setting file was set as default. I corrected it. Now it is working.
How to manage local vs production settings in Django?
In settings.py
:
try:
from local_settings import *
except ImportError as e:
pass
You can override what needed in local_settings.py
; it should stay out of your version control then. But since you mention copying I'm guessing you use none ;) How to properly manage django production and development files
hope you are well.
To answer the following questions I will do so based in the format you have asked.
You ask about management of production and development files with source control (namely GitHub). It would be best to store these in different branches of source control. Example: "main" branch being used for production and a "development" branch being used for development. This will allow you to work with both branches and you can merge development branch into the production branch.
The best way you can manage sensitive information such as passwords and keys in source control is to avail of
.env
files (What is the use of python-dotenv?), which stores variables in an environment. You can store variables in this file and tell GitHub to ignore this file in the.gitignore
file.You mention ideal file structure. There are many ways which files can be structured and normally I would say this is preference on the developers behalf as this doesn't really matter as long as the developer and future developers can make sense of the file structure. From personal recommendation and this is my own opinion.
- staticfiles folder
- projectName
- settings.py etc
- app
- app files
- manage.py
Have a good day.
What is the recommended method for deploying Django settings to a production environment?
There is not one recommended way of deploying a Django app. The official documentation provides a guide for deploying with Apache and mod_wsgi. Some other options are a PaaS like Heroku/Dokku or deployment with Docker
It's common to divide your settings in different files. You could for example divide settings in four different files:
- base file (base.py) - Common settings for all environments (every file below imports from this with
from .base import *
- development file (development.py) - settings specific for development, (DEBUG = True etc...)
- production file (production.py) - settings specific for the production environment. (DEBUG = False, whitenoise configuration etc)
- testing file (testing.py)
DJANGO_SETTINGS_MODULE
It is also common recommended practice that you store secrets like the SECRET_KEY in environment variables. SECRET_KEY = os.environ.get('SECRET_KEY', None)
. Or check out the django-environ package.
Check out this repo for an example setup.
Init.py goes for local settings.py rather than production settings.py DJANGO
Update an env keys
Try putting a key like environment in env file as
ENVIRONMENT = TEST
And use manage.py to select the suitable settings for production or local settings.Use if case. I have done this in my real life deployable project, and it works in both production and local environment.
I have these files in my settings folder
and I had updated my manage.py like these
Here dep is the environment key which says whether the server is in development or deployment.
Conclusion
Add a key for the current environment and select the settings wisely.Related Topics
Django/Python Beginner: Error When Executing Python Manage.Py Syncdb - Psycopg2 Not Found
Generate Rfc 3339 Timestamp in Python
Django - Makemigrations - No Changes Detected
How to Save Final Model Using Keras
How to Set Opacity of Background Colour of Graph with Matplotlib
Does Python Evaluate If's Conditions Lazily
What Do Backticks Mean to the Python Interpreter? Example: 'Num'
Tuple Unpacking Order Changes Values Assigned
How to Define a Class Constant Inside an Enum
Python Memory Usage of Numpy Arrays
Running a Command as a Super User from a Python Script
How to Delete Created Variables, Functions, etc from the Memory of the Interpreter
How to Concatenate Element-Wise Two Lists in Python
Should All Python Classes Extend Object
Memory Error When Using Pandas Read_Csv
Selenium Webdriver in Python - Files Download Directory Change in Chrome Preferences