middleware.txt 7.4 KB

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