middleware.txt 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 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. If :setting:`PREPEND_WWW` is ``True``, URLs that lack a leading "www."
  39. will be redirected to the same URL with a leading "www."
  40. Both of these options are meant to normalize URLs. The philosophy is that
  41. each URL should exist in one, and only one, place. Technically a URL
  42. ``foo.com/bar`` is distinct from ``foo.com/bar/`` -- a search-engine
  43. indexer would treat them as separate URLs -- so it's best practice to
  44. normalize URLs.
  45. * Handles ETags based on the :setting:`USE_ETAGS` setting. If
  46. :setting:`USE_ETAGS` is set to ``True``, Django will calculate an ETag
  47. for each request by MD5-hashing the page content, and it'll take care of
  48. sending ``Not Modified`` responses, if appropriate.
  49. .. class:: BrokenLinkEmailsMiddleware
  50. * Sends broken link notification emails to :setting:`MANAGERS` (see
  51. :doc:`/howto/error-reporting`).
  52. View metadata middleware
  53. ------------------------
  54. .. module:: django.middleware.doc
  55. :synopsis: Middleware to help your app self-document.
  56. .. class:: XViewMiddleware
  57. Sends custom ``X-View`` HTTP headers to HEAD requests that come from IP
  58. addresses defined in the :setting:`INTERNAL_IPS` setting. This is used by
  59. Django's :doc:`automatic documentation system </ref/contrib/admin/admindocs>`.
  60. Depends on :class:`~django.contrib.auth.middleware.AuthenticationMiddleware`.
  61. GZip middleware
  62. ---------------
  63. .. module:: django.middleware.gzip
  64. :synopsis: Middleware to serve GZipped content for performance.
  65. .. class:: GZipMiddleware
  66. Compresses content for browsers that understand GZip compression (all modern
  67. browsers).
  68. This middleware should be placed before any other middleware that need to
  69. read or write the response body so that compression happens afterward.
  70. It will NOT compress content if any of the following are true:
  71. * The content body is less than 200 bytes long.
  72. * The response has already set the ``Content-Encoding`` header.
  73. * The request (the browser) hasn't sent an ``Accept-Encoding`` header
  74. containing ``gzip``.
  75. * The request is from Internet Explorer and the ``Content-Type`` header
  76. contains ``javascript`` or starts with anything other than ``text/``.
  77. We do this to avoid a bug in early versions of IE that caused decompression
  78. not to be performed on certain content types.
  79. You can apply GZip compression to individual views using the
  80. :func:`~django.views.decorators.gzip.gzip_page()` decorator.
  81. Conditional GET middleware
  82. --------------------------
  83. .. module:: django.middleware.http
  84. :synopsis: Middleware handling advanced HTTP features.
  85. .. class:: ConditionalGetMiddleware
  86. Handles conditional GET operations. If the response has a ``ETag`` or
  87. ``Last-Modified`` header, and the request has ``If-None-Match`` or
  88. ``If-Modified-Since``, the response is replaced by an
  89. :class:`~django.http.HttpResponseNotModified`.
  90. Also sets the ``Date`` and ``Content-Length`` response-headers.
  91. Reverse proxy middleware
  92. ------------------------
  93. .. class:: SetRemoteAddrFromForwardedFor
  94. This middleware was removed in Django 1.1. See :ref:`the release notes
  95. <removed-setremoteaddrfromforwardedfor-middleware>` for details.
  96. Locale middleware
  97. -----------------
  98. .. module:: django.middleware.locale
  99. :synopsis: Middleware to enable language selection based on the request.
  100. .. class:: LocaleMiddleware
  101. Enables language selection based on data from the request. It customizes
  102. content for each user. See the :doc:`internationalization documentation
  103. </topics/i18n/translation>`.
  104. Message middleware
  105. ------------------
  106. .. module:: django.contrib.messages.middleware
  107. :synopsis: Message middleware.
  108. .. class:: MessageMiddleware
  109. Enables cookie- and session-based message support. See the
  110. :doc:`messages documentation </ref/contrib/messages>`.
  111. Session middleware
  112. ------------------
  113. .. module:: django.contrib.sessions.middleware
  114. :synopsis: Session middleware.
  115. .. class:: SessionMiddleware
  116. Enables session support. See the :doc:`session documentation
  117. </topics/http/sessions>`.
  118. Authentication middleware
  119. -------------------------
  120. .. module:: django.contrib.auth.middleware
  121. :synopsis: Authentication middleware.
  122. .. class:: AuthenticationMiddleware
  123. Adds the ``user`` attribute, representing the currently-logged-in user, to
  124. every incoming ``HttpRequest`` object. See :ref:`Authentication in Web requests
  125. <auth-web-requests>`.
  126. CSRF protection middleware
  127. --------------------------
  128. .. module:: django.middleware.csrf
  129. :synopsis: Middleware adding protection against Cross Site Request
  130. Forgeries.
  131. .. class:: CsrfViewMiddleware
  132. Adds protection against Cross Site Request Forgeries by adding hidden form
  133. fields to POST forms and checking requests for the correct value. See the
  134. :doc:`Cross Site Request Forgery protection documentation </ref/contrib/csrf>`.
  135. Transaction middleware
  136. ----------------------
  137. .. module:: django.middleware.transaction
  138. :synopsis: Middleware binding a database transaction to each Web request.
  139. .. class:: TransactionMiddleware
  140. .. versionchanged:: 1.6
  141. ``TransactionMiddleware`` is deprecated. The documentation of transactions
  142. contains :ref:`upgrade instructions <transactions-upgrading-from-1.5>`.
  143. Binds commit and rollback of the default database to the request/response
  144. phase. If a view function runs successfully, a commit is done. If it fails with
  145. an exception, a rollback is done.
  146. The order of this middleware in the stack is important: middleware modules
  147. running outside of it run with commit-on-save - the default Django behavior.
  148. Middleware modules running inside it (coming later in the stack) will be under
  149. the same transaction control as the view functions.
  150. See the :doc:`transaction management documentation </topics/db/transactions>`.
  151. X-Frame-Options middleware
  152. --------------------------
  153. .. module:: django.middleware.clickjacking
  154. :synopsis: Clickjacking protection
  155. .. class:: XFrameOptionsMiddleware
  156. Simple :doc:`clickjacking protection via the X-Frame-Options header </ref/clickjacking/>`.