2
0

settings.txt 10 KB

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