views:

60

answers:

1

Currently I have this in my settings.py file:

DEBUG = True
LOCAL = True
TEMPLATE_DEBUG = DEBUG

SITE_TITLE = 'Stack Overflow Question'

REMOTE_SITE_URL = "http://************:8080"
LOCAL_SITE_URL  = "http://************:8000"

ADMINS = (
    # ('Your Name', '[email protected]'),
)

MANAGERS = ADMINS

if LOCAL:
    SITE_URL = LOCAL_SITE_URL
    ROOT_URL = '/mnt/hgfs/Sites/************'
    engine = 'sqlite3'
    dbName = '************.db'
    dbUser = ''
    dbPassword = ''
    dbHost = ''
    dbPort = ''
else:
    SITE_URL = REMOTE_SITE_URL
    ROOT_URL = '/var/www/vhosts/www.*************.com/public/'
    engine = 'mysql'
    dbName = '************'
    dbUser = 'www-data'
    dbPassword = '************'
    dbHost = ''
    dbPort = ''

The idea is that if I modify LOCAL=True to false, I can switch between the sqlite3 database and the mysql that's kept on the server. But this is cumbersome when using SVN. What I want is the ability for settings.py to intelligently know if it's on the server or running locally. Is there a way to sniff the URL, IP or simply check a file that exists on the server and not locally? Not only am I looking for a solution, but one that doesn't tax the server. Checking if a file exists might be a bit heavy of a process if it occurs every time Django renders a page. Any suggestions?

+3  A: 

Checking if a file exists will not occur every time a render occurs. It will actually only occur whenever you interpreter process is started, which all depends on your deployment configuration. This will depend on a variety on your webserver setup, but if you are using apache, chiefly MaxRequestPerChild and StartServers, and related parameters. For example, if I have

StartServers 8
MaxrequestsPerChild 4000

in a preforked apache, I will test that file for the first 8 requests, listen to 32,000 more requests, then test that file 8 more times. (Yes, I know it's much more complicated than this.)

The point is, most implementations that load your code will not re-source the settings.py file very often. With that in mind, how about:

import os
if os.uname()[1] == 'my.development.server.com':
    LOCAL = True
else:
    LOCAL = False
David Berger
Perfect! Thanks again for the quick response. I think this works out great.
Sebastian