Multiple django configurations for development and production

Matthew Nuzum —  — 3 Comments

In Rails you automatically get different configurations for development, testing and production. In Django there’s no clear way to do this so here’s how Marc Tardif (a coworker) and I got it working.

Our use case is as follows: We work together on the same programming project but since we’re in different countries (and on different LANs) we have different development configurations. Our production server for the applications we co-develop is yet a different configuration. Furthermore, we use [version control](http://bazaar-vcs.org) to keep track of each others’ work. When I’m not working on a team project I still often run a different configuration on my local pc than I do on the server where I publish my app.

Django’s manage.py allows you to pass the parameter –settings=something whenever you use it. This is nice but limiting because most of the configuration is the same for our three configurations. Only a few lines differ (mostly relating to database config).

Therefore there is a stock settings.py file where most of the configuration lives. Then for each developer (or each different configuration) there is a settings_[hostname].py file. So for me there is settings_matts-laptop.py. There is also a settings_production.py used for production.

The settings_[hostname].py file gets the customizations needed for me. At the top of this file is the line:

from settings import *

Then I run django like this:

./manage.py –setings_matts-laptop runserver

Here’s a __complete example__:

from settings import *

DATABASE_ENGINE = ‘sqlite3′
DATABASE_NAME = ‘/home/matt/Projects/trainingwebsite/database.sqlite’

EMAIL_HOST = ‘mail.mchsi.com’

TIME_ZONE = ‘America/Chicago’

MEDIA_ROOT = ‘/home/matt/Projects/trainingwebsite/media/’
MEDIA_URL = ‘http://localhost/files/’
TEMPLATE_DIRS = (
# Always use forward slashes, even on Windows.
# Don’t forget to use absolute paths, not relative paths.
“/home/matt/Projects/trainingwebsite”,
)

Your apache mod_python config for production will look something like this:

SetHandler python-program
PythonPath “[‘/srv/site-name/python-packages’] + sys.path”
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE trainingwebsite.settings_production

The above assumes your django application lives in the directory /srv/site-name/python-packages/trainingwebsite.

If it helped, please share!Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+

Matthew Nuzum

Posts Twitter Facebook

Web guy, big thinker, loves to talk and write. Front end web, mobile and UX developer for John Deere ISG. My projects: @dsmwebgeeks @tekrs @squaretap ✝
  • http://www.markstahler.ca Mark Stahler

    Cant believe all I needed was ‘from settings import *’

    WOOT

  • Vladimir Konkov

    profile_name = os.environ.get(“SETTINGS_PROFILE”, False)

    if profile_name:
    exec ‘from settings_%s import *’ % (profile_name,)

  • Anonymous

    I’m having some issues with “from settings import *”. Settings are not all correctly imported after the project is deployed.

    I’m wondering if someone ran into that issue too.