Django development and production environments

One of the easiest solution to divide django settings for production and development environment is to create separete files for every environment. For example:

  • defaults.py
  • dev.py
  • production.py

Common settings put into defaults.py.

But from the beggining.. create in your app folder (this is the place where you can find settings.py) folder named "settings". In this folder put a file named "__init__.py" and set there development environment as a default:

from my_app.settings.dev import *

As the next step create a file "defaults.py" with settings common for all environments.

In "dev.py" import defaults settings:

from my_app.settings.defaults import *

DEBUG = True

FILER_DEBUG = True

FILER_ENABLE_LOGGING = True

#and all other development specific settings, for example locale db settings

In "production.py" you also should import defaults settings:

from my_app.settings.defaults import *

DEBUG = False

#and other production sepcific settings for example production db server settings

Your my_app folder should look like this:

  • my_app/
    • settings/
      • __init__.py
      • defaults.py
      • dev.py
      • production.py

When development environment was set as a default, your production server should tell django to turn on production settings. With apache server I did it with "wsgi.py":

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_app.settings.production")

Add this file in git ignore list, so when you update your code, git will not upload your local "wsgi.py".

Second important thing could be MEDIA_ROOT and STATICFILES_DIRS path. When I've moved settings into settings folder those paths changed, so I set them like this: '../media' '../my_static'.