middleware.txt 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. ==========
  2. Middleware
  3. ==========
  4. .. module:: django.middleware
  5. :synopsis: Django's built-in middleware classes.
  6. This document explains all middleware components that come with Django. For
  7. information on how how to use them and how to write your own middleware, see
  8. the :doc:`middleware usage guide </topics/http/middleware>`.
  9. Available middleware
  10. ====================
  11. Cache middleware
  12. ----------------
  13. .. module:: django.middleware.cache
  14. :synopsis: Middleware for the site-wide cache.
  15. .. class:: UpdateCacheMiddleware
  16. .. class:: FetchFromCacheMiddleware
  17. Enable the site-wide cache. If these are enabled, each Django-powered page will
  18. be cached for as long as the :setting:`CACHE_MIDDLEWARE_SECONDS` setting
  19. defines. See the :doc:`cache documentation </topics/cache>`.
  20. "Common" middleware
  21. -------------------
  22. .. module:: django.middleware.common
  23. :synopsis: Middleware adding "common" conveniences for perfectionists.
  24. .. class:: CommonMiddleware
  25. Adds a few conveniences for perfectionists:
  26. * Forbids access to user agents in the :setting:`DISALLOWED_USER_AGENTS`
  27. setting, which should be a list of strings.
  28. * Performs URL rewriting based on the :setting:`APPEND_SLASH` and
  29. :setting:`PREPEND_WWW` settings.
  30. If :setting:`APPEND_SLASH` is ``True`` and the initial URL doesn't end
  31. with a slash, and it is not found in the URLconf, then a new URL is
  32. formed by appending a slash at the end. If this new URL is found in the
  33. URLconf, then Django redirects the request to this new URL. Otherwise,
  34. the initial URL is processed as usual.
  35. For example, ``foo.com/bar`` will be redirected to ``foo.com/bar/`` if
  36. you don't have a valid URL pattern for ``foo.com/bar`` but *do* have a
  37. valid pattern for ``foo.com/bar/``.
  38. .. versionchanged:: 1.0
  39. The behavior of :setting:`APPEND_SLASH` has changed slightly in this
  40. version. It didn't used to check whether the pattern was matched in
  41. the URLconf.
  42. If :setting:`PREPEND_WWW` is ``True``, URLs that lack a leading "www."
  43. will be redirected to the same URL with a leading "www."
  44. Both of these options are meant to normalize URLs. The philosophy is that
  45. each URL should exist in one, and only one, place. Technically a URL
  46. ``foo.com/bar`` is distinct from ``foo.com/bar/`` -- a search-engine
  47. indexer would treat them as separate URLs -- so it's best practice to
  48. normalize URLs.
  49. * Sends broken link notification emails to :setting:`MANAGERS` if
  50. :setting:`SEND_BROKEN_LINK_EMAILS` is set to ``True``.
  51. * Handles ETags based on the :setting:`USE_ETAGS` setting. If
  52. :setting:`USE_ETAGS` is set to ``True``, Django will calculate an ETag
  53. for each request by MD5-hashing the page content, and it'll take care of
  54. sending ``Not Modified`` responses, if appropriate.
  55. View metadata middleware
  56. ------------------------
  57. .. module:: django.middleware.doc
  58. :synopsis: Middleware to help your app self-document.
  59. .. class:: XViewMiddleware
  60. Sends custom ``X-View`` HTTP headers to HEAD requests that come from IP
  61. addresses defined in the :setting:`INTERNAL_IPS` setting. This is used by
  62. Django's :doc:`automatic documentation system </ref/contrib/admin/admindocs>`.
  63. GZIP middleware
  64. ---------------
  65. .. module:: django.middleware.gzip
  66. :synopsis: Middleware to serve gziped content for performance.
  67. .. class:: GZipMiddleware
  68. Compresses content for browsers that understand gzip compression (all modern
  69. browsers).
  70. It is suggested to place this first in the middleware list, so that the
  71. compression of the response content is the last thing that happens. Will not
  72. compress content bodies less than 200 bytes long, when the response code is
  73. something other than 200, JavaScript files (for IE compatibility), or
  74. responses that have the ``Content-Encoding`` header already specified.
  75. Conditional GET middleware
  76. --------------------------
  77. .. module:: django.middleware.http
  78. :synopsis: Middleware handling advanced HTTP features.
  79. .. class:: ConditionalGetMiddleware
  80. Handles conditional GET operations. If the response has a ``ETag`` or
  81. ``Last-Modified`` header, and the request has ``If-None-Match`` or
  82. ``If-Modified-Since``, the response is replaced by an
  83. :class:`~django.http.HttpNotModified`.
  84. Also sets the ``Date`` and ``Content-Length`` response-headers.
  85. Reverse proxy middleware
  86. ------------------------
  87. .. class:: SetRemoteAddrFromForwardedFor
  88. .. versionchanged:: 1.1
  89. This middleware was removed in Django 1.1. See :ref:`the release notes
  90. <removed-setremoteaddrfromforwardedfor-middleware>` for details.
  91. Locale middleware
  92. -----------------
  93. .. module:: django.middleware.locale
  94. :synopsis: Middleware to enable language selection based on the request.
  95. .. class:: LocaleMiddleware
  96. Enables language selection based on data from the request. It customizes
  97. content for each user. See the :doc:`internationalization documentation
  98. </topics/i18n/index>`.
  99. Message middleware
  100. ------------------
  101. .. module:: django.contrib.messages.middleware
  102. :synopsis: Message middleware.
  103. .. class:: MessageMiddleware
  104. .. versionadded:: 1.2
  105. ``MessageMiddleware`` was added.
  106. Enables cookie- and session-based message support. See the
  107. :doc:`messages documentation </ref/contrib/messages>`.
  108. Session middleware
  109. ------------------
  110. .. module:: django.contrib.sessions.middleware
  111. :synopsis: Session middleware.
  112. .. class:: SessionMiddleware
  113. Enables session support. See the :doc:`session documentation
  114. </topics/http/sessions>`.
  115. Authentication middleware
  116. -------------------------
  117. .. module:: django.contrib.auth.middleware
  118. :synopsis: Authentication middleware.
  119. .. class:: AuthenticationMiddleware
  120. Adds the ``user`` attribute, representing the currently-logged-in user, to
  121. every incoming ``HttpRequest`` object. See :doc:`Authentication in Web requests
  122. </topics/auth>`.
  123. CSRF protection middleware
  124. --------------------------
  125. .. module:: django.middleware.csrf
  126. :synopsis: Middleware adding protection against Cross Site Request
  127. Forgeries.
  128. .. class:: CsrfMiddleware
  129. .. versionadded:: 1.0
  130. Adds protection against Cross Site Request Forgeries by adding hidden form
  131. fields to POST forms and checking requests for the correct value. See the
  132. :doc:`Cross Site Request Forgery protection documentation </ref/contrib/csrf>`.
  133. Transaction middleware
  134. ----------------------
  135. .. module:: django.middleware.transaction
  136. :synopsis: Middleware binding a database transaction to each Web request.
  137. .. class:: TransactionMiddleware
  138. Binds commit and rollback to the request/response phase. If a view function
  139. runs successfully, a commit is done. If it fails with an exception, a rollback
  140. is done.
  141. The order of this middleware in the stack is important: middleware modules
  142. running outside of it run with commit-on-save - the default Django behavior.
  143. Middleware modules running inside it (coming later in the stack) will be under
  144. the same transaction control as the view functions.
  145. See the :doc:`transaction management documentation </topics/db/transactions>`.