middleware.txt 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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:: django.middleware.cache.UpdateCacheMiddleware
  16. .. class:: django.middleware.cache.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:: django.middleware.common.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. * Handles ETags based on the :setting:`USE_ETAGS` setting. If
  50. :setting:`USE_ETAGS` is set to ``True``, Django will calculate an ETag
  51. for each request by MD5-hashing the page content, and it'll take care of
  52. sending ``Not Modified`` responses, if appropriate.
  53. View metadata middleware
  54. ------------------------
  55. .. module:: django.middleware.doc
  56. :synopsis: Middleware to help your app self-document.
  57. .. class:: django.middleware.doc.XViewMiddleware
  58. Sends custom ``X-View`` HTTP headers to HEAD requests that come from IP
  59. addresses defined in the :setting:`INTERNAL_IPS` setting. This is used by
  60. Django's automatic documentation system.
  61. GZIP middleware
  62. ---------------
  63. .. module:: django.middleware.gzip
  64. :synopsis: Middleware to serve gziped content for performance.
  65. .. class:: django.middleware.gzip.GZipMiddleware
  66. Compresses content for browsers that understand gzip compression (all modern
  67. browsers).
  68. It is suggested to place this first in the middleware list, so that the
  69. compression of the response content is the last thing that happens. Will not
  70. compress content bodies less than 200 bytes long, when the response code is
  71. something other than 200, JavaScript files (for IE compatibility), or
  72. responses that have the ``Content-Encoding`` header already specified.
  73. Conditional GET middleware
  74. --------------------------
  75. .. module:: django.middleware.http
  76. :synopsis: Middleware handling advanced HTTP features.
  77. .. class:: django.middleware.http.ConditionalGetMiddleware
  78. Handles conditional GET operations. If the response has a ``ETag`` or
  79. ``Last-Modified`` header, and the request has ``If-None-Match`` or
  80. ``If-Modified-Since``, the response is replaced by an
  81. :class:`~django.http.HttpNotModified`.
  82. Also sets the ``Date`` and ``Content-Length`` response-headers.
  83. Reverse proxy middleware
  84. ------------------------
  85. .. class:: django.middleware.http.SetRemoteAddrFromForwardedFor
  86. .. versionchanged:: 1.1
  87. This middleware was removed in Django 1.1. See :ref:`the release notes
  88. <removed-setremoteaddrfromforwardedfor-middleware>` for details.
  89. Locale middleware
  90. -----------------
  91. .. module:: django.middleware.locale
  92. :synopsis: Middleware to enable language selection based on the request.
  93. .. class:: django.middleware.locale.LocaleMiddleware
  94. Enables language selection based on data from the request. It customizes
  95. content for each user. See the :doc:`internationalization documentation
  96. </topics/i18n/index>`.
  97. Message middleware
  98. ------------------
  99. .. module:: django.contrib.messages.middleware
  100. :synopsis: Message middleware.
  101. .. class:: django.contrib.messages.middleware.MessageMiddleware
  102. .. versionadded:: 1.2
  103. ``MessageMiddleware`` was added.
  104. Enables cookie- and session-based message support. See the
  105. :doc:`messages documentation </ref/contrib/messages>`.
  106. Session middleware
  107. ------------------
  108. .. module:: django.contrib.sessions.middleware
  109. :synopsis: Session middleware.
  110. .. class:: django.contrib.sessions.middleware.SessionMiddleware
  111. Enables session support. See the :doc:`session documentation
  112. </topics/http/sessions>`.
  113. Authentication middleware
  114. -------------------------
  115. .. module:: django.contrib.auth.middleware
  116. :synopsis: Authentication middleware.
  117. .. class:: django.contrib.auth.middleware.AuthenticationMiddleware
  118. Adds the ``user`` attribute, representing the currently-logged-in user, to
  119. every incoming ``HttpRequest`` object. See :doc:`Authentication in Web requests
  120. </topics/auth>`.
  121. CSRF protection middleware
  122. --------------------------
  123. .. module:: django.middleware.csrf
  124. :synopsis: Middleware adding protection against Cross Site Request
  125. Forgeries.
  126. .. class:: django.middleware.csrf.CsrfMiddleware
  127. .. versionadded:: 1.0
  128. Adds protection against Cross Site Request Forgeries by adding hidden form
  129. fields to POST forms and checking requests for the correct value. See the
  130. :doc:`Cross Site Request Forgery protection documentation </ref/contrib/csrf>`.
  131. Transaction middleware
  132. ----------------------
  133. .. module:: django.middleware.transaction
  134. :synopsis: Middleware binding a database transaction to each Web request.
  135. .. class:: django.middleware.transaction.TransactionMiddleware
  136. Binds commit and rollback to the request/response phase. If a view function
  137. runs successfully, a commit is done. If it fails with an exception, a rollback
  138. is done.
  139. The order of this middleware in the stack is important: middleware modules
  140. running outside of it run with commit-on-save - the default Django behavior.
  141. Middleware modules running inside it (coming later in the stack) will be under
  142. the same transaction control as the view functions.
  143. See the :doc:`transaction management documentation </topics/db/transactions>`.