14Oct

Rails-like environments with Django.

Posted by Elf Sternberg as django

I needed some rails-like environment settings using Django.  This is the quick and easy way to get that done.   First, after building your “settings.py” file, create an environment under your project root named “env“.   Move your settings.py file to this directory, rename it “development.py"

Now, in your root directory, open up a new file named “settings.py” and put this in:

import os
import os.path

PROJECT_ROOT = os.path.normpath(os.path.dirname(__file__))

local_import = "env/development.py"
if os.getenv("DJANGO_ENV") == 'TEST':
    local_import = "env/test.py"
elif os.getenv("DJANGO_ENV") == 'PRODUCTION':
    local_import = "env/production.py"

import_file = open(os.path.join(PROJECT_ROOT, local_import))
exec(import_file)

Bingo!  Now you have different versions of settings.py depending upon whether or not you’re starting the server in TEST, PRODUCTION, or the default, “development”.  Create production.py and test.py as needed.  You can start the server with:

DJANGO_ENV=TEST ./manage.py runserver

And it will load the correct environment.  Using a shell script to run a test server and then a test harness might not be the most elegant thing in the world, but at least it’s Un*x, and it makes development less stressy.

Sweet!

2 Responses to Rails-like environments with Django.

David

October 15th, 2009 at 1:48 am

I don’t get it, why would you actually do this:
DJANGO_ENV=PRODUCTION ./manage.py runserver

The django server is not for production…

Elf Sternberg

October 20th, 2009 at 10:58 am

Well, presumably you wouldn’t want to do that, but you would want to set the environment variable for what database to use and what mailer to use, etc., under different conditions, no?

Comment Form

Subscribe to Feed

Categories

Calendar

October 2009
M T W T F S S
« Sep   Nov »
 1234
567891011
12131415161718
19202122232425
262728293031