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 resides. Then create a file in settings folder, so the folder can be treated as a python module. Rename the original to and move it into settings folder. Create a file and copy common settings among different environments from into 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 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 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 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.