middleware.txt 7.2 KB

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