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