settings.txt 10 KB

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