settings.txt 10 KB

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