PageRenderTime 81ms CodeModel.GetById 47ms RepoModel.GetById 2ms app.codeStats 0ms

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