Login

Read settings from local_settings.py

Author:
statico
Posted:
January 18, 2010
Language:
Python
Version:
1.1
Score:
6 (after 6 ratings)

Each installation of our Django site has slightly different settings -- namely, which database to use. Developers can provide a local_settings.py file which lets them override (or, just as usefully, extend) settings that are in settings.py.

Subversion is told to ignore local_settings.py, so it's never checked in. If local_settings.py is missing, the site refuses to work.

We include a local_settings_example.py file so that new developers can get started more quickly.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#########################################################################
# Import settings from local_settings.py, if it exists.
#
# Put this at the end of settings.py

try:
  import local_settings
except ImportError:
  print """ 
    -------------------------------------------------------------------------
    You need to create a local_settings.py file which needs to contain at least
    database connection information.
    
    Copy local_settings_example.py to local_settings.py and edit it.
    -------------------------------------------------------------------------
    """
  import sys 
  sys.exit(1)
else:
  # Import any symbols that begin with A-Z. Append to lists any symbols that
  # begin with "EXTRA_".
  import re
  for attr in dir(local_settings):
    match = re.search('^EXTRA_(\w+)', attr)
    if match:
      name = match.group(1)
      value = getattr(local_settings, attr)
      try:
        globals()[name] += value
      except KeyError:
        globals()[name] = value
    elif re.search('^[A-Z]', attr):
      globals()[attr] = getattr(local_settings, attr)

#########################################################################
# local_settings_example.py

# Local Django settings example.
#
# You can put any custom options you would normally want in settings.py into
# this file. If you want to *add* middleware or applications, precede a setting
# name with "EXTRA_".

# Use a local SQLite database.
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = '/tmp/djangodb'
DATABASE_USER = ''
DATABASE_PASSWORD = ''
DATABASE_HOST = ''
DATABASE_PORT = ''

# Required for Django Debug Toolbar and other things.
INTERNAL_IPS = ('127.0.0.1',)

# Add Django Debug Toolbar application. (It's cool.)
EXTRA_MIDDLEWARE_CLASSES = ( 
    'debug_toolbar.middleware.DebugToolbarMiddleware',
)

EXTRA_INSTALLED_APPS = ( 
    'debug_toolbar',
)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.