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