5 A Django settings file contains all the configuration of your Django
6 installation. This document explains how settings work and which settings are
12 A settings file is just a Python module with module-level variables.
14 Here are a couple of example settings::
17 DEFAULT_FROM_EMAIL = 'webmaster@example.com'
18 TEMPLATE_DIRS = ('/home/templates/mike', '/home/templates/john')
20 Because a settings file is a Python module, the following apply:
22 * It shouldn't have Python syntax errors.
23 * It can assign settings dynamically using normal Python syntax.
26 MY_SETTING = [str(i) for i in range(30)]
28 * It can import values from other settings files.
30 Designating the settings
31 ========================
33 When you use Django, you have to tell it which settings you're using. Do this
34 by using an environment variable, ``DJANGO_SETTINGS_MODULE``.
36 The value of ``DJANGO_SETTINGS_MODULE`` should be in Python path syntax, e.g.
37 ``"myproject.settings"``. Note that the settings module should be on the
38 Python `import search path`_.
40 .. _import search path: http://diveintopython.org/getting_to_know_python/everything_is_an_object.html
42 The django-admin.py utility
43 ---------------------------
45 When using `django-admin.py`_, you can either set the environment variable
46 once, or explicitly pass in the settings module each time you run the utility.
48 Example (Unix Bash shell)::
50 export DJANGO_SETTINGS_MODULE=myproject.settings
51 django-admin.py runserver
53 Example (Windows shell)::
55 set DJANGO_SETTINGS_MODULE=myproject.settings
56 django-admin.py runserver
58 Use the ``--settings`` command-line argument to specify the settings manually::
60 django-admin.py runserver --settings=myproject.settings
62 .. _django-admin.py: http://www.djangoproject.com/documentation/django_admin/
64 On the server (mod_python)
65 --------------------------
67 In your live server environment, you'll need to tell Apache/mod_python which
68 settings file to use. Do that with ``SetEnv``::
71 SetHandler python-program
72 PythonHandler django.core.handlers.modpython
73 SetEnv DJANGO_SETTINGS_MODULE myproject.settings
76 Read the `Django mod_python documentation`_ for more information.
78 .. _Django mod_python documentation: http://www.djangoproject.com/documentation/mod_python/
83 A Django settings file doesn't have to define any settings if it doesn't need
84 to. Each setting has a sensible default value. These defaults live in the file
85 ``django/conf/global_settings.py``.
87 Here's the algorithm Django uses in compiling settings:
89 * Load settings from ``global_settings.py``.
90 * Load settings from the specified settings file, overriding the global
91 settings as necessary.
93 Note that a settings file should *not* import from ``global_settings``, because
96 Using settings in Python code
97 =============================
99 In your Django apps, use settings by importing them from
100 ``django.conf.settings``. Example::
102 from django.conf.settings import DEBUG
107 Note that your code should *not* import from either ``global_settings`` or
108 your own settings file. ``django.conf.settings`` abstracts the concepts of
109 default settings and site-specific settings; it presents a single interface.
111 Altering settings at runtime
112 ============================
114 You shouldn't alter settings in your applications at runtime. For example,
115 don't do this in a view::
117 from django.conf.settings import DEBUG
119 DEBUG = True # Don't do this!
121 The only place you should assign to settings is in a settings file.
126 Because a settings file contains sensitive information, such as the database
127 password, you should make every attempt to limit access to it. For example,
128 change its file permissions so that only you and your Web server's user can
129 read it. This is especially important in a shared-hosting environment.
134 Here's a full list of all available settings, in alphabetical order, and their
137 ABSOLUTE_URL_OVERRIDES
138 ----------------------
140 Default: ``{}`` (Empty dictionary)
142 A dictionary mapping ``"app_label.module_name"`` strings to functions that take
143 a model object and return its URL. This is a way of overriding
144 ``get_absolute_url()`` methods on a per-installation basis. Example::
146 ABSOLUTE_URL_OVERRIDES = {
147 'blogs.blogs': lambda o: "/blogs/%s/" % o.slug,
148 'news.stories': lambda o: "/stories/%s/%s/" % (o.pub_year, o.slug),
154 Default: ``()`` (Empty list)
156 Used for admin-site settings modules, this should be a tuple of settings
157 modules (in the format ``'foo.bar.baz'``) for which this site is an admin.
162 Default: ``'/media/'``
164 The URL prefix for admin media -- CSS, JavaScript and images. Make sure to use
170 Default: ``()`` (Empty tuple)
172 A tuple that lists people who get code error notifications. When
173 ``DEBUG=False`` and a view raises an exception, Django will e-mail these people
174 with the full exception information. Each member of the tuple should be a tuple
175 of (Full name, e-mail address). Example::
177 (('John', 'john@example.com'), ('Mary', 'mary@example.com'))
179 ALLOWED_INCLUDE_ROOTS
180 ---------------------
182 Default: ``()`` (Empty tuple)
184 A tuple of strings representing allowed prefixes for the ``{% ssi %}`` template
185 tag. This is a security measure, so that template authors can't access files
186 that they shouldn't be accessing.
188 For example, if ``ALLOWED_INCLUDE_ROOTS`` is ``('/home/html', '/var/www')``,
189 then ``{% ssi /home/html/foo.txt %}`` would work, but ``{% ssi /etc/passwd %}``
197 Whether to append trailing slashes to URLs. This is only used if
198 ``CommonMiddleware`` is installed (see the `middleware docs`_). See also
204 Default: ``'simple://'``
206 The cache backend to use. See the `cache docs`_.
208 CACHE_MIDDLEWARE_KEY_PREFIX
210 Default: ``''`` (Empty string)
212 The cache key prefix that the cache middleware should use. See the
218 Default: ``'postgresql'``
220 Which database backend to use. Either ``'postgresql'``, ``'mysql'``,
221 ``'sqlite3'`` or ``'ado_mssql'``.
226 Default: ``''`` (Empty string)
228 Which host to use when connecting to the database. An empty string means
229 localhost. Not used with SQLite.
234 Default: ``''`` (Empty string)
236 The name of the database to use. For SQLite, it's the full path to the database
242 Default: ``''`` (Empty string)
244 The password to use when connecting to the database. Not used with SQLite.
249 Default: ``''`` (Empty string)
251 The port to use when connecting to the database. An empty string means the
252 default port. Not used with SQLite.
257 Default: ``''`` (Empty string)
259 The username to use when connecting to the database. Not used with SQLite.
264 Default: ``'N j, Y'`` (e.g. ``Feb. 4, 2003``)
266 The default formatting to use for date fields on Django admin change-list
267 pages -- and, possibly, by other parts of the system. See
268 `allowed date format strings`_.
270 See also DATETIME_FORMAT and TIME_FORMAT.
272 .. _allowed date format strings: http://www.djangoproject.com/documentation/templates/#now
277 Default: ``'N j, Y, P'`` (e.g. ``Feb. 4, 2003, 4 p.m.``)
279 The default formatting to use for datetime fields on Django admin change-list
280 pages -- and, possibly, by other parts of the system. See
281 `allowed date format strings`_.
283 See also DATE_FORMAT and TIME_FORMAT.
285 .. _allowed date format strings: http://www.djangoproject.com/documentation/templates/#now
292 A boolean that turns on/off debug mode.
299 Default charset to use for all ``HttpResponse`` objects, if a MIME type isn't
300 manually specified. Used with ``DEFAULT_CONTENT_TYPE`` to construct the
301 ``Content-Type`` header.
306 Default: ``'text/html'``
308 Default content type to use for all ``HttpResponse`` objects, if a MIME type
309 isn't manually specified. Used with ``DEFAULT_CHARSET`` to construct the
310 ``Content-Type`` header.
315 Default: ``'webmaster@localhost'``
317 Default e-mail address to use for various automated correspondence from the
320 DISALLOWED_USER_AGENTS
321 ----------------------
323 Default: ``()`` (Empty tuple)
325 List of compiled regular expression objects representing User-Agent strings
326 that are not allowed to visit any page, systemwide. Use this for bad
327 robots/crawlers. This is only used if ``CommonMiddleware`` is installed (see
328 the `middleware docs`_).
333 Default: ``'localhost'``
335 The host to use for sending e-mail.
340 Default: ``'[Django] '``
342 Subject-line prefix for e-mail messages sent with ``django.core.mail.mail_admins``
343 or ``django.core.mail.mail_managers``. You'll probably want to include the
349 Default: ``('mail.pl', 'mailform.pl', 'mail.cgi', 'mailform.cgi', 'favicon.ico', '.php')``
351 See also ``IGNORABLE_404_STARTS``.
356 Default: ``('/cgi-bin/', '/_vti_bin', '/_vti_inf')``
358 A tuple of strings that specify beginnings of URLs that should be ignored by
359 the 404 e-mailer. See ``SEND_BROKEN_LINK_EMAILS`` and ``IGNORABLE_404_ENDS``.
364 Default: ``()`` (Empty tuple)
366 A tuple of strings designating all applications that are enabled in this Django
367 installation. Each string should be a full Python path to a Python package that
368 contains a Django application, as created by `django-admin.py startapp`_.
370 .. _django-admin.py startapp: http://www.djangoproject.com/documentation/django_admin/#startapp-appname
375 Default: ``()`` (Empty tuple)
377 A tuple of IP addresses, as strings, that:
379 * See debug comments, when ``DEBUG`` is ``True``
380 * Receive X headers if the ``XViewMiddleware`` is installed (see the
386 Default: ``'/usr/bin/jing'``
388 Path to the "Jing" executable. Jing is a RELAX NG validator, and Django uses it
389 to validate each ``XMLField`` in your models.
390 See http://www.thaiopensource.com/relaxng/jing.html .
397 A string representing the language code for this installation. This should be
398 in standard language format. For example, U.S. English is ``"en-us"``. See the
399 `internationalization docs`_.
401 .. _internationalization docs: http://www.djangoproject.com/documentation/i18n/
406 Default: A tuple of all available languages. Currently, this is::
409 ('bn', _('Bengali')),
414 ('en', _('English')),
415 ('es', _('Spanish')),
417 ('gl', _('Galician')),
418 ('is', _('Icelandic')),
419 ('it', _('Italian')),
420 ('no', _('Norwegian')),
421 ('pt-br', _('Brazilian')),
422 ('ro', _('Romanian')),
423 ('ru', _('Russian')),
425 ('sr', _('Serbian')),
426 ('sv', _('Swedish')),
427 ('zh-cn', _('Simplified Chinese')),
430 A tuple of two-tuples in the format (language code, language name). This
431 specifies which languages are available for language selection. See the
432 `internationalization docs`_ for details.
434 Generally, the default value should suffice. Only set this setting if you want
435 to restrict language selection to a subset of the Django-provided languages.
440 Default: ``ADMINS`` (Whatever ``ADMINS`` is set to)
442 A tuple in the same format as ``ADMINS`` that specifies who should get
443 broken-link notifications when ``SEND_BROKEN_LINK_EMAILS=True``.
448 Default: ``''`` (Empty string)
450 Absolute path to the directory that holds media for this installation.
451 Example: ``"/home/media/media.lawrence.com/"`` See also ``MEDIA_URL``.
456 Default: ``''`` (Empty string)
458 URL that handles the media served from ``MEDIA_ROOT``.
459 Example: ``"http://media.lawrence.com"``
466 ("django.middleware.sessions.SessionMiddleware",
467 "django.middleware.common.CommonMiddleware",
468 "django.middleware.doc.XViewMiddleware")
470 A tuple of middleware classes to use. See the `middleware docs`_.
477 Whether to prepend the "www." subdomain to URLs that don't have it. This is
478 only used if ``CommonMiddleware`` is installed (see the `middleware docs`_).
479 See also ``APPEND_SLASH``.
484 Default: ``''`` (Empty string)
486 A secret key for this particular Django installation. Used to provide a seed in
487 secret-key hashing algorithms. Set this to a random string -- the longer, the
488 better. ``django-admin.py startproject`` creates one automatically.
490 SEND_BROKEN_LINK_EMAILS
491 -----------------------
495 Whether to send an e-mail to the ``MANAGERS`` each time somebody visits a
496 Django-powered page that is 404ed with a non-empty referer (i.e., a broken
497 link). This is only used if ``CommonMiddleware`` is installed (see the
498 `middleware docs`_). See also ``IGNORABLE_404_STARTS`` and
499 ``IGNORABLE_404_ENDS``.
504 Default: ``'root@localhost'``
506 The e-mail address that error messages come from, such as those sent to
507 ``ADMINS`` and ``MANAGERS``.
512 Default: ``1209600`` (2 weeks, in seconds)
514 The age of session cookies, in seconds. See the `session docs`_.
516 SESSION_COOKIE_DOMAIN
517 ---------------------
521 The domain to use for session cookies. Set this to a string such as
522 ``".lawrence.com"`` for cross-domain cookies, or use ``None`` for a standard
523 domain cookie. See the `session docs`_.
528 Default: ``'hotclub'``
530 The name of the cookie to use for sessions. This can be whatever you want.
531 See the `session docs`_.
533 ``'hotclub'`` is a reference to the Hot Club of France, the band Django
536 SESSION_SAVE_EVERY_REQUEST
537 --------------------------
541 Whether to save the session data on every request. See the `session docs`_.
548 The ID, as an integer, of the current site in the ``sites`` database. This is
549 used so that application data can hook into specific site(s) and a single
550 database can manage content for multiple sites.
557 **Only available in Django development version.**
559 A boolean that turns on/off template debug mode. If this is ``True``, the fancy
560 error page will display a detailed report for any ``TemplateSyntaxError``. This
561 report contains the relevant snippet of the template, with the appropriate line
564 Note that Django only displays fancy error pages if ``DEBUG`` is ``True``, so you'll
565 want to set that to take advantage of this setting.
572 Default: ``()`` (Empty tuple)
574 List of locations of the template source files, in search order. See the
575 `template documentation`_.
577 TEMPLATE_FILE_EXTENSION
578 -----------------------
582 The file extension to append to all template names when searching for
583 templates. See the `template documentation`_.
588 Default: ``('django.core.template.loaders.filesystem.load_template_source',)``
590 A tuple of callables (as strings) that know how to import templates from
591 various sources. See the `template documentation`_.
596 Default: ``'P'`` (e.g. ``4 p.m.``)
598 The default formatting to use for time fields on Django admin change-list
599 pages -- and, possibly, by other parts of the system. See
600 `allowed date format strings`_.
602 See also DATE_FORMAT and DATETIME_FORMAT.
604 .. _allowed date format strings: http://www.djangoproject.com/documentation/templates/#now
609 Default: ``'America/Chicago'``
611 A string representing the time zone for this installation. `See available choices`_.
613 Note that this is the time zone to which Django will convert all dates/times --
614 not necessarily the timezone of the server. For example, one server may serve
615 multiple Django-powered sites, each with a separate time-zone setting.
622 A boolean that specifies whether to output the "Etag" header. This saves
623 bandwidth but slows down performance. This is only used if ``CommonMiddleware``
624 is installed (see the `middleware docs`_).
626 .. _cache docs: http://www.djangoproject.com/documentation/cache/
627 .. _middleware docs: http://www.djangoproject.com/documentation/middleware/
628 .. _session docs: http://www.djangoproject.com/documentation/sessions/
629 .. _See available choices: http://www.postgresql.org/docs/current/static/datetime-keywords.html#DATETIME-TIMEZONE-SET-TABLE
630 .. _template documentation: http://www.djangoproject.com/documentation/templates_python/
632 Creating your own settings
633 ==========================
635 There's nothing stopping you from creating your own settings, for your own
636 Django apps. Just follow these conventions:
638 * Setting names are in all uppercase.
639 * For settings that are sequences, use tuples instead of lists. This is
640 purely for performance.
641 * Don't reinvent an already-existing setting.