elfs: (Default)
[personal profile] elfs

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!

This entry was automatically cross-posted from Elf's technical journal, ElfSternberg.com

Profile

elfs: (Default)
Elf Sternberg

December 2025

S M T W T F S
 12345 6
78910111213
14151617181920
21222324252627
28293031   

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jan. 1st, 2026 09:03 am
Powered by Dreamwidth Studios