Settings file switcher

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import socket, platform
hostname = socket.gethostname().replace('.','_').replace('-', '_')

try:
  import os
  path = "settings_files/%s.py" % hostname
  path = os.path.join(os.path.dirname(__file__), path)
  execfile(path)
except ImportError, e:
  import sys
  sys.stderr.write("Unable to read settings_files/%s.py\n" % hostname)
  sys.exit(1)

More like this

  1. Different approach to local settings.py by jokull 5 years, 3 months ago
  2. Setting distinction between development and public server by Archatas 6 years, 2 months ago
  3. language switcher in admin by lawgon 3 years, 6 months ago
  4. Change model field attribute in a DRY way by jedie 1 year, 7 months ago
  5. Add model form field meta data in a DRY way by jedie 4 years, 2 months ago

Comments

itavor (on April 3, 2008):

A variation:

import socket
hostname = socket.gethostname().replace('-', '_').split('.')
while True:
    try:
        exec 'from host_settings.%s import *' % '_'.join(hostname)
        break
    except ImportError:
        hostname = hostname[:-1]

Lets you use shorter names - for example, mysite.py for mysite.com.au, or www_mysite.py for www.mysite.com.

#

(Forgotten your password?)