settings.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. 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 :source:`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. The command ``python manage.py diffsettings`` displays differences between the
  70. 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 :doc:`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, but follow these guidelines:
  107. * Setting names must be all uppercase.
  108. * Don't reinvent an already-existing setting.
  109. For settings that are sequences, Django itself uses lists, but this is only
  110. a convention.
  111. .. _settings-without-django-settings-module:
  112. Using settings without setting :envvar:`DJANGO_SETTINGS_MODULE`
  113. ===============================================================
  114. In some cases, you might want to bypass the :envvar:`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:
  120. .. function:: django.conf.settings.configure(default_settings, **settings)
  121. Example::
  122. from django.conf import settings
  123. settings.configure(DEBUG=True)
  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. documentation of :setting:`TIME_ZONE` for why this would normally occur). It's
  135. assumed that you're already in full control of your environment in these
  136. cases.
  137. Custom default settings
  138. -----------------------
  139. If you'd like default values to come from somewhere other than
  140. ``django.conf.global_settings``, you can pass in a module or class that
  141. provides the default settings as the ``default_settings`` argument (or as the
  142. first positional argument) in the call to ``configure()``.
  143. In this example, default settings are taken from ``myapp_defaults``, and the
  144. :setting:`DEBUG` setting is set to ``True``, regardless of its value in
  145. ``myapp_defaults``::
  146. from django.conf import settings
  147. from myapp import myapp_defaults
  148. settings.configure(default_settings=myapp_defaults, DEBUG=True)
  149. The following example, which uses ``myapp_defaults`` as a positional argument,
  150. is equivalent::
  151. settings.configure(myapp_defaults, DEBUG=True)
  152. Normally, you will not need to override the defaults in this fashion. The
  153. Django defaults are sufficiently tame that you can safely use them. Be aware
  154. that if you do pass in a new default module, it entirely *replaces* the Django
  155. defaults, so you must specify a value for every possible setting that might be
  156. used in that code you are importing. Check in
  157. ``django.conf.settings.global_settings`` for the full list.
  158. Either ``configure()`` or :envvar:`DJANGO_SETTINGS_MODULE` is required
  159. ----------------------------------------------------------------------
  160. If you're not setting the :envvar:`DJANGO_SETTINGS_MODULE` environment
  161. variable, you *must* call ``configure()`` at some point before using any code
  162. that reads settings.
  163. If you don't set :envvar:`DJANGO_SETTINGS_MODULE` and don't call
  164. ``configure()``, Django will raise an ``ImportError`` exception the first time
  165. a setting is accessed.
  166. If you set :envvar:`DJANGO_SETTINGS_MODULE`, access settings values somehow,
  167. *then* call ``configure()``, Django will raise a ``RuntimeError`` indicating
  168. that settings have already been configured. There is a property for this
  169. purpose:
  170. .. attribute:: django.conf.settings.configured
  171. For example::
  172. from django.conf import settings
  173. if not settings.configured:
  174. settings.configure(myapp_defaults, DEBUG=True)
  175. Also, it's an error to call ``configure()`` more than once, or to call
  176. ``configure()`` after any setting has been accessed.
  177. It boils down to this: Use exactly one of either ``configure()`` or
  178. :envvar:`DJANGO_SETTINGS_MODULE`. Not both, and not neither.
  179. Calling ``django.setup()`` is required for "standalone" Django usage
  180. --------------------------------------------------------------------
  181. If you're using components of Django "standalone" -- for example, writing a
  182. Python script which loads some Django templates and renders them, or uses the
  183. ORM to fetch some data -- there's one more step you'll need in addition to
  184. configuring settings.
  185. After you've either set :envvar:`DJANGO_SETTINGS_MODULE` or called
  186. ``configure()``, you'll need to call :func:`django.setup()` to load your
  187. settings and populate Django's application registry. For example::
  188. import django
  189. from django.conf import settings
  190. from myapp import myapp_defaults
  191. settings.configure(default_settings=myapp_defaults, DEBUG=True)
  192. django.setup()
  193. # Now this script or any imported module can use any part of Django it needs.
  194. from myapp import models
  195. Note that calling ``django.setup()`` is only necessary if your code is truly
  196. standalone. When invoked by your web server, or through :doc:`django-admin
  197. </ref/django-admin>`, Django will handle this for you.
  198. .. admonition:: ``django.setup()`` may only be called once.
  199. Therefore, avoid putting reusable application logic in standalone scripts
  200. so that you have to import from the script elsewhere in your application.
  201. If you can't avoid that, put the call to ``django.setup()`` inside an
  202. ``if`` block::
  203. if __name__ == '__main__':
  204. import django
  205. django.setup()
  206. .. seealso::
  207. :doc:`The Settings Reference </ref/settings>`
  208. Contains the complete list of core and contrib app settings.