Changes between Initial Version and Version 1 of SplitSettings


Ignore:
Timestamp:
Aug 8, 2006, 2:17:15 PM (18 years ago)
Author:
Steven Armstrong
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • SplitSettings

    v1 v1  
     1= Splitting up the settings file =
     2
     3If you use a source control system (CVS, SVN, ...), or want to publish your application on the web,
     4it may be a good idea to move sensitive or machine/user specific settings like database
     5passwords and such out of the main settings.py file.
     6
     7As discussions on the django-developers mailing list have shown everybody has different
     8requirements and ideas how to do this. This page is ment to collect some of these ideas for future reference.
     9
     10One thing to keep in mind is that django's config files are pure python.
     11This gives you the ultimate flexibility to handle configurations the way you think is best.
     12Or to quote Adrian Holovaty:
     13{{{
     14We don't need a default solution for this. It's not within the scope
     15of this project to tell people how they should organize their settings
     16files. Take that opportunity to showcase your individualism.
     17}}}
     18
     19
     20== Different settings in different files ==
     21I believe the first user who came up with this was Hugo, who used this method in the projects he published on [https://simon.bofh.ms/cgi-bin/trac-django-projects.cgi/ his site].
     22
     23{{{
     24#!python
     25
     26SECRET_KEY = open(os.path.expanduser('~/.gallery-secret')).read().strip()
     27
     28}}}
     29
     30
     31== ini-style file for deployment ==
     32This is a solution that Michael Radziej posted to django-developers. His motivation was to be able to store the settings
     33for an app he published under /etc with all the other system config files.
     34
     35/etc/whatever/settings.ini
     36{{{
     37[database]
     38DATABASE_USER: bla
     39DATABASE_PASSWORD: XXXXXXXX
     40DATABASE_HOST: dev
     41DATABASE_PORT:
     42DATABASE_ENGINE: mysql
     43DATABASE_NAME: blo
     44TESTSUITE_DATABASE_NAME: test_blo
     45
     46[secrets]
     47SECRET_KEY: random-string-of-ascii
     48CSRF_MIDDLEWARE_SECRET: random-string-of-ascii
     49
     50[cookies]
     51SESSION_COOKIE_DOMAIN:
     52
     53# all settings in debug section should be false in productive
     54environment
     55# INTERNAL_IPS should be empty in productive environment
     56[debug]
     57DEBUG: true
     58TEMPLATE_DEBUG: true
     59VIEW_TEST: true
     60INTERNAL_IPS: 127.0.0.1
     61SKIP_CSRF_MIDDLEWARE: true
     62
     63[email]
     64SERVER_EMAIL: django@localhost
     65EMAIL_HOST: localhost
     66
     67# the [error mail] and [404 mail] sections are special. Just add
     68lines with
     69#  full name: email_address@domain.xx
     70# each section must be present but may be empty.
     71[error mail]
     72Adam Smith: adam@localhost
     73
     74[404 mail]
     75John Wayne: john@localhost
     76
     77}}}
     78
     79
     80/path/to/whatever/settings.py
     81{{{
     82#!python
     83
     84from ConfigParser import RawConfigParser
     85
     86config = RawConfigParser()
     87config.read('/etc/whatever/settings.ini')
     88
     89DATABASE_USER = config.get('database', 'DATABASE_USER')
     90DATABASE_PASSWORD = config.get('database', 'DATABASE_PASSWORD')
     91DATABASE_HOST = config.get('database', 'DATABASE_HOST')
     92DATABASE_PORT = config.get('database', 'DATABASE_PORT')
     93DATABASE_ENGINE = config.get('database', 'DATABASE_ENGINE')
     94DATABASE_NAME = config.get('database', 'DATABASE_NAME')
     95TEST_DATABASE_NAME = config.get('database', 'TESTSUITE_DATABASE_NAME')
     96
     97SECRET_KEY = config.get('secrets','SECRET_KEY')
     98CSRF_MIDDLEWARE_SECRET = config.get('secrets', 'CSRF_MIDDLEWARE_SECRET')
     99
     100SESSION_COOKIE_DOMAIN = config.get('cookies','SESSION_COOKIE_DOMAIN')
     101
     102DEBUG = config.getboolean('debug','DEBUG')
     103TEMPLATE_DEBUG = config.getboolean('debug','TEMPLATE_DEBUG')
     104VIEW_TEST = config.getboolean('debug', 'VIEW_TEST')
     105INTERNAL_IPS = tuple(config.get('debug', 'INTERNAL_IPS').split())
     106if config.getboolean('debug', 'SKIP_CSRF_MIDDLEWARE'):
     107    MIDDLEWARE_CLASSES = tuple([x for x in list(MIDDLEWARE_CLASSES)
     108                                  if not x.endswith('CsrfMiddleware')])
     109
     110SERVER_EMAIL = config.get('email', 'SERVER_EMAIL')
     111EMAIL_HOST = config.get('email', 'EMAIL_HOST')
     112ADMINS = tuple(config.items('error mail'))
     113MANAGERS = tuple(config.items('404 mail'))
     114
     115}}}
     116
     117
     118== Multiple setting files importing from each other ==
     119This is my (Steven Armstrong) preferred solution.
     120
     121Keep application wide, unsensitive settings and sane defaults in your normal settings.py file.
     122
     123/path/to/whatever/settings.py
     124{{{
     125#!python
     126import os
     127
     128BASE_DIR = os.path.dirname(os.path.abspath(__file__))
     129#PROJECT_DIR = os.path.abspath(os.path.join(BASE_DIR, '..'))
     130PROJECT_DIR = BASE_DIR
     131
     132DEBUG = False
     133TEMPLATE_DEBUG = DEBUG
     134
     135ADMINS = (
     136    ('Mr Sysadmin', 'sysadmin@domain.tld'),
     137)
     138
     139MANAGERS = ADMINS
     140
     141DATABASE_ENGINE = 'sqlite3'
     142DATABASE_NAME = os.path.join(PROJECT_DIR, 'project.db')
     143DATABASE_USER = ''
     144DATABASE_PASSWORD = ''
     145DATABASE_HOST = ''
     146DATABASE_PORT = ''
     147
     148TIME_ZONE = 'Europe/Zurich'
     149LANGUAGE_CODE = 'en-us'
     150SECRET_KEY = 'secret'
     151
     152#[more default and app wide settings]
     153
     154from settings_local import *
     155}}}
     156
     157At the end of your normal settings.py include * from an other, machine specific config file
     158which could look something like this.
     159
     160/path/to/whatever/settings_local.py
     161{{{
     162#!python
     163
     164DEBUG = True
     165TEMPLATE_DEBUG = DEBUG
     166
     167# don't want emails while developing
     168ADMINS = ()
     169MANAGERS = ADMINS
     170
     171DATABASE_ENGINE = 'mysql'
     172DATABASE_NAME = 'mydbname'
     173DATABASE_USER = 'mydbuser'
     174DATABASE_PASSWORD = 'mydbpassword'
     175DATABASE_HOST = 'localhost'
     176DATABASE_PORT = ''
     177
     178SECRET_KEY = 'random-string-of-ascii'
     179
     180#[more user/machine specific settings]
     181}}}
     182
     183settings.py goes into CVS, SVN, (put your favorite RCS here)[[BR]]
     184settings_local.py does _not_ go under RCS
     185
     186If wanted a settings_local.template file can be put under version control with
     187instructions to copy it over to settings_local.py, change it to suite the environment,
     188and to never ever commit it to the RCS system.
Back to Top