settings.txt 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. .. _topics-settings:
  2. ===============
  3. Django settings
  4. ===============
  5. A Django settings file contains all the configuration of your Django
  6. installation. This document explains how settings work and which settings are
  7. available.
  8. The basics
  9. ==========
  10. A settings file is just a Python module with module-level variables.
  11. Here are a couple of example settings::
  12. DEBUG = False
  13. DEFAULT_FROM_EMAIL = 'webmaster@example.com'
  14. TEMPLATE_DIRS = ('/home/templates/mike', '/home/templates/john')
  15. Because a settings file is a Python module, the following apply:
  16. * It doesn't allow for Python syntax errors.
  17. * It can assign settings dynamically using normal Python syntax.
  18. For example::
  19. MY_SETTING = [str(i) for i in range(30)]
  20. * It can import values from other settings files.
  21. .. _django-settings-module:
  22. Designating the settings
  23. ========================
  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.org/getting_to_know_python/everything_is_an_object.html
  30. The django-admin.py utility
  31. ---------------------------
  32. When using :ref:`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_python)
  45. --------------------------
  46. In your live server environment, you'll need to tell Apache/mod_python which
  47. settings file to use. Do that with ``SetEnv``::
  48. <Location "/mysite/">
  49. SetHandler python-program
  50. PythonHandler django.core.handlers.modpython
  51. SetEnv DJANGO_SETTINGS_MODULE mysite.settings
  52. </Location>
  53. Read the :ref:`Django mod_python documentation <howto-deployment-modpython>` for
  54. more information.
  55. Default settings
  56. ================
  57. A Django settings file doesn't have to define any settings if it doesn't need
  58. to. Each setting has a sensible default value. These defaults live in the file
  59. ``django/conf/global_settings.py``.
  60. Here's the algorithm Django uses in compiling settings:
  61. * Load settings from ``global_settings.py``.
  62. * Load settings from the specified settings file, overriding the global
  63. settings as necessary.
  64. Note that a settings file should *not* import from ``global_settings``, because
  65. that's redundant.
  66. Seeing which settings you've changed
  67. ------------------------------------
  68. There's an easy way to view which of your settings deviate from the default
  69. settings. The command ``python manage.py diffsettings`` displays differences
  70. between the current settings file and Django's default settings.
  71. For more, see the :djadmin:`diffsettings` documentation.
  72. Using settings in Python code
  73. =============================
  74. In your Django apps, use settings by importing the object
  75. ``django.conf.settings``. Example::
  76. from django.conf import settings
  77. if settings.DEBUG:
  78. # Do something
  79. Note that ``django.conf.settings`` isn't a module -- it's an object. So
  80. importing individual settings is not possible::
  81. from django.conf.settings import DEBUG # This won't work.
  82. Also note that your code should *not* import from either ``global_settings`` or
  83. your own settings file. ``django.conf.settings`` abstracts the concepts of
  84. default settings and site-specific settings; it presents a single interface.
  85. It also decouples the code that uses settings from the location of your
  86. settings.
  87. Altering settings at runtime
  88. ============================
  89. You shouldn't alter settings in your applications at runtime. For example,
  90. don't do this in a view::
  91. from django.conf import settings
  92. settings.DEBUG = True # Don't do this!
  93. The only place you should assign to settings is in a settings file.
  94. Security
  95. ========
  96. Because a settings file contains sensitive information, such as the database
  97. password, you should make every attempt to limit access to it. For example,
  98. change its file permissions so that only you and your Web server's user can
  99. read it. This is especially important in a shared-hosting environment.
  100. Available settings
  101. ==================
  102. For a full list of available settings, see the :ref:`settings reference <ref-settings>`.
  103. Creating your own settings
  104. ==========================
  105. There's nothing stopping you from creating your own settings, for your own
  106. Django apps. Just follow these conventions:
  107. * Setting names are in all uppercase.
  108. * Don't reinvent an already-existing setting.
  109. For settings that are sequences, Django itself uses tuples, rather than lists,
  110. but this is only a convention.
  111. .. _settings-without-django-settings-module:
  112. Using settings without setting DJANGO_SETTINGS_MODULE
  113. =====================================================
  114. In some cases, you might want to bypass the ``DJANGO_SETTINGS_MODULE``
  115. environment variable. For example, if you're using the template system by
  116. itself, you likely don't want to have to set up an environment variable
  117. pointing to a settings module.
  118. In these cases, you can configure Django's settings manually. Do this by
  119. calling ``django.conf.settings.configure()``.
  120. Example::
  121. from django.conf import settings
  122. settings.configure(DEBUG=True, TEMPLATE_DEBUG=True,
  123. TEMPLATE_DIRS=('/home/web-apps/myapp', '/home/web-apps/base'))
  124. Pass ``configure()`` as many keyword arguments as you'd like, with each keyword
  125. argument representing a setting and its value. Each argument name should be all
  126. uppercase, with the same name as the settings described above. If a particular
  127. setting is not passed to ``configure()`` and is needed at some later point,
  128. Django will use the default setting value.
  129. Configuring Django in this fashion is mostly necessary -- and, indeed,
  130. recommended -- when you're using a piece of the framework inside a larger
  131. application.
  132. Consequently, when configured via ``settings.configure()``, Django will not
  133. make any modifications to the process environment variables. (See the
  134. explanation of ``TIME_ZONE``, above, for why this would normally occur.) It's
  135. assumed that you're already in full control of your environment in these 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. ``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.
  168. Also, it's an error to call ``configure()`` more than once, or to call
  169. ``configure()`` after any setting has been accessed.
  170. It boils down to this: Use exactly one of either ``configure()`` or
  171. ``DJANGO_SETTINGS_MODULE``. Not both, and not neither.
  172. .. _@login_required: ../authentication/#the-login-required-decorator