How to Manage Django Settings for Production and Development

When I started developing with django and deploying django application on a server, I was facing the problem of how to mange different django settings for different environments with ease. I found many questions on stackoverflow like this and blog posts like this regarding this topic. They are informative but just don’t fit my style quite well, so after summing up their solutions, I came up with my own idea.

Create a folder called settings in the same level where the original settings.py resides. Then create a __init__.py file in settings folder, so the folder can be treated as a python module. Rename the original settings.py to settings_default.py and move it into settings folder. Create a common.py file and copy common settings among different environments from settings_default.py into common.py. Then create one file per environment. Currently I only have two environments which are development and production. Put environment specific settings like DEBUG = True into those files. Right now folder structure should be like this:

As I put settings one level deeper down the folder structure, I need to change the BASE_DIR definition in common.py to point to the original position, so this line is like: BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), i.e. an extra  os.path.dirname() call added.

Then add these lines to __init__.py file under setting folder:


so the the correct settings is loaded when the django app runs.

As the above init file looks for a “DJANGO_MODULE_STR” environment variable, set this variable in local development computer by export DJANGO_MODULE_STR=development. This way after the development work is done I can just git push to push the most recent code to a git repo, and pull it in the production server from that repo.

Leave a Reply

Your email address will not be published. Required fields are marked *