settings.txt 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. ===============
  2. Django settings
  3. ===============
  4. A Django settings file contains all the configuration of your Django
  5. installation. This document explains how settings work and which settings are
  6. available.
  7. The basics
  8. ==========
  9. A settings file is just a Python module with module-level variables.
  10. Here are a couple of example settings::
  11. DEBUG = False
  12. DEFAULT_FROM_EMAIL = 'webmaster@example.com'
  13. TEMPLATE_DIRS = ('/home/templates/mike', '/home/templates/john')
  14. Because a settings file is a Python module, the following apply:
  15. * It doesn't allow for Python syntax errors.
  16. * It can assign settings dynamically using normal Python syntax.
  17. For example::
  18. MY_SETTING = [str(i) for i in range(30)]
  19. * It can import values from other settings files.
  20. .. _django-settings-module:
  21. Designating the settings
  22. ========================
  23. .. envvar:: DJANGO_SETTINGS_MODULE
  24. When you use Django, you have to tell it which settings you're using. Do this
  25. by using an environment variable, ``DJANGO_SETTINGS_MODULE``.
  26. The value of ``DJANGO_SETTINGS_MODULE`` should be in Python path syntax, e.g.
  27. ``mysite.settings``. Note that the settings module should be on the
  28. Python `import search path`_.
  29. .. _import search path: http://diveintopython.net/getting_to_know_python/everything_is_an_object.html
  30. The django-admin.py utility
  31. ---------------------------
  32. When using :doc:`django-admin.py </ref/django-admin>`, you can either set the
  33. environment variable once, or explicitly pass in the settings module each time
  34. you run the utility.
  35. Example (Unix Bash shell)::
  36. export DJANGO_SETTINGS_MODULE=mysite.settings
  37. django-admin.py runserver
  38. Example (Windows shell)::
  39. set DJANGO_SETTINGS_MODULE=mysite.settings
  40. django-admin.py runserver
  41. Use the ``--settings`` command-line argument to specify the settings manually::
  42. django-admin.py runserver --settings=mysite.settings
  43. .. _django-admin.py: ../django-admin/
  44. On the server (mod_wsgi)
  45. --------------------------
  46. In your live server environment, you'll need to tell your WSGI
  47. application what settings file to use. Do that with ``os.environ``::
  48. import os
  49. os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
  50. Read the :doc:`Django mod_wsgi documentation
  51. </howto/deployment/wsgi/modwsgi>` for more information and other common
  52. elements to a Django WSGI application.
  53. Default settings
  54. ================
  55. A Django settings file doesn't have to define any settings if it doesn't need
  56. to. Each setting has a sensible default value. These defaults live in the
  57. module :file:`django/conf/global_settings.py`.
  58. Here's the algorithm Django uses in compiling settings:
  59. * Load settings from ``global_settings.py``.
  60. * Load settings from the specified settings file, overriding the global
  61. settings as necessary.
  62. Note that a settings file should *not* import from ``global_settings``, because
  63. that's redundant.
  64. Seeing which settings you've changed
  65. ------------------------------------
  66. There's an easy way to view which of your settings deviate from the default
  67. settings. The command ``python manage.py diffsettings`` displays differences
  68. between the current settings file and Django's default settings.
  69. For more, see the :djadmin:`diffsettings` documentation.
  70. Using settings in Python code
  71. =============================
  72. In your Django apps, use settings by importing the object
  73. ``django.conf.settings``. Example::
  74. from django.conf import settings
  75. if settings.DEBUG:
  76. # Do something
  77. Note that ``django.conf.settings`` isn't a module -- it's an object. So
  78. importing individual settings is not possible::
  79. from django.conf.settings import DEBUG # This won't work.
  80. Also note that your code should *not* import from either ``global_settings`` or
  81. your own settings file. ``django.conf.settings`` abstracts the concepts of
  82. default settings and site-specific settings; it presents a single interface.
  83. It also decouples the code that uses settings from the location of your
  84. settings.
  85. Altering settings at runtime
  86. ============================
  87. You shouldn't alter settings in your applications at runtime. For example,
  88. don't do this in a view::
  89. from django.conf import settings
  90. settings.DEBUG = True # Don't do this!
  91. The only place you should assign to settings is in a settings file.
  92. Security
  93. ========
  94. Because a settings file contains sensitive information, such as the database
  95. password, you should make every attempt to limit access to it. For example,
  96. change its file permissions so that only you and your Web server's user can
  97. read it. This is especially important in a shared-hosting environment.
  98. Available settings
  99. ==================
  100. For a full list of available settings, see the :doc:`settings reference </ref/settings>`.
  101. Creating your own settings
  102. ==========================
  103. There's nothing stopping you from creating your own settings, for your own
  104. Django apps. Just follow these conventions:
  105. * Setting names are in all uppercase.
  106. * Don't reinvent an already-existing setting.
  107. For settings that are sequences, Django itself uses tuples, rather than lists,
  108. but this is only a convention.
  109. .. _settings-without-django-settings-module:
  110. Using settings without setting DJANGO_SETTINGS_MODULE
  111. =====================================================
  112. In some cases, you might want to bypass the ``DJANGO_SETTINGS_MODULE``
  113. environment variable. For example, if you're using the template system by
  114. itself, you likely don't want to have to set up an environment variable
  115. pointing to a settings module.
  116. In these cases, you can configure Django's settings manually. Do this by
  117. calling:
  118. .. function:: django.conf.settings.configure(default_settings, **settings)
  119. Example::
  120. from django.conf import settings
  121. settings.configure(DEBUG=True, TEMPLATE_DEBUG=True,
  122. TEMPLATE_DIRS=('/home/web-apps/myapp', '/home/web-apps/base'))
  123. Pass ``configure()`` as many keyword arguments as you'd like, with each keyword
  124. argument representing a setting and its value. Each argument name should be all
  125. uppercase, with the same name as the settings described above. If a particular
  126. setting is not passed to ``configure()`` and is needed at some later point,
  127. Django will use the default setting value.
  128. Configuring Django in this fashion is mostly necessary -- and, indeed,
  129. recommended -- when you're using a piece of the framework inside a larger
  130. application.
  131. Consequently, when configured via ``settings.configure()``, Django will not
  132. make any modifications to the process environment variables (see the
  133. documentation of :setting:`TIME_ZONE` for why this would normally occur). It's
  134. assumed that you're already in full control of your environment in these
  135. cases.
  136. Custom default settings
  137. -----------------------
  138. If you'd like default values to come from somewhere other than
  139. ``django.conf.global_settings``, you can pass in a module or class that
  140. provides the default settings as the ``default_settings`` argument (or as the
  141. first positional argument) in the call to ``configure()``.
  142. In this example, default settings are taken from ``myapp_defaults``, and the
  143. :setting:`DEBUG` setting is set to ``True``, regardless of its value in
  144. ``myapp_defaults``::
  145. from django.conf import settings
  146. from myapp import myapp_defaults
  147. settings.configure(default_settings=myapp_defaults, DEBUG=True)
  148. The following example, which uses ``myapp_defaults`` as a positional argument,
  149. is equivalent::
  150. settings.configure(myapp_defaults, DEBUG=True)
  151. Normally, you will not need to override the defaults in this fashion. The
  152. Django defaults are sufficiently tame that you can safely use them. Be aware
  153. that if you do pass in a new default module, it entirely *replaces* the Django
  154. defaults, so you must specify a value for every possible setting that might be
  155. used in that code you are importing. Check in
  156. ``django.conf.settings.global_settings`` for the full list.
  157. Either configure() or DJANGO_SETTINGS_MODULE is required
  158. --------------------------------------------------------
  159. If you're not setting the ``DJANGO_SETTINGS_MODULE`` environment variable, you
  160. *must* call ``configure()`` at some point before using any code that reads
  161. settings.
  162. If you don't set ``DJANGO_SETTINGS_MODULE`` and don't call ``configure()``,
  163. Django will raise an ``ImportError`` exception the first time a setting
  164. is accessed.
  165. If you set ``DJANGO_SETTINGS_MODULE``, access settings values somehow, *then*
  166. call ``configure()``, Django will raise a ``RuntimeError`` indicating
  167. that settings have already been configured. There is a property just for this
  168. purpose:
  169. .. attribute: django.conf.settings.configured
  170. For example::
  171. from django.conf import settings
  172. if not settings.configured:
  173. settings.configure(myapp_defaults, DEBUG=True)
  174. Also, it's an error to call ``configure()`` more than once, or to call
  175. ``configure()`` after any setting has been accessed.
  176. It boils down to this: Use exactly one of either ``configure()`` or
  177. ``DJANGO_SETTINGS_MODULE``. Not both, and not neither.
  178. .. _@login_required: ../authentication/#the-login-required-decorator