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
David
October 15th, 2009 at 1:48 am
1I 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
2Well, 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?
RSS feed for comments on this post · TrackBack URI
Leave a reply
Categories
Archives
Links
Meta
Calendar