middleware.txt 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. 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. GZIP middleware
  60. ---------------
  61. .. module:: django.middleware.gzip
  62. :synopsis: Middleware to serve gziped content for performance.
  63. .. class:: GZipMiddleware
  64. Compresses content for browsers that understand gzip compression (all modern
  65. browsers).
  66. It is suggested to place this first in the middleware list, so that the
  67. compression of the response content is the last thing that happens. Will not
  68. compress content bodies less than 200 bytes long, when the response code is
  69. something other than 200, JavaScript files (for IE compatibility), or
  70. responses that have the ``Content-Encoding`` header already specified.
  71. GZip compression can be applied to individual views using the
  72. :func:`~django.views.decorators.http.gzip_page()` decorator.
  73. Conditional GET middleware
  74. --------------------------
  75. .. module:: django.middleware.http
  76. :synopsis: Middleware handling advanced HTTP features.
  77. .. class:: 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:: SetRemoteAddrFromForwardedFor
  86. This middleware was removed in Django 1.1. See :ref:`the release notes
  87. <removed-setremoteaddrfromforwardedfor-middleware>` for details.
  88. Locale middleware
  89. -----------------
  90. .. module:: django.middleware.locale
  91. :synopsis: Middleware to enable language selection based on the request.
  92. .. class:: LocaleMiddleware
  93. Enables language selection based on data from the request. It customizes
  94. content for each user. See the :doc:`internationalization documentation
  95. </topics/i18n/index>`.
  96. Message middleware
  97. ------------------
  98. .. module:: django.contrib.messages.middleware
  99. :synopsis: Message middleware.
  100. .. class:: MessageMiddleware
  101. .. versionadded:: 1.2
  102. ``MessageMiddleware`` was added.
  103. Enables cookie- and session-based message support. See the
  104. :doc:`messages documentation </ref/contrib/messages>`.
  105. Session middleware
  106. ------------------
  107. .. module:: django.contrib.sessions.middleware
  108. :synopsis: Session middleware.
  109. .. class:: SessionMiddleware
  110. Enables session support. See the :doc:`session documentation
  111. </topics/http/sessions>`.
  112. Authentication middleware
  113. -------------------------
  114. .. module:: django.contrib.auth.middleware
  115. :synopsis: Authentication middleware.
  116. .. class:: AuthenticationMiddleware
  117. Adds the ``user`` attribute, representing the currently-logged-in user, to
  118. every incoming ``HttpRequest`` object. See :doc:`Authentication in Web requests
  119. </topics/auth>`.
  120. CSRF protection middleware
  121. --------------------------
  122. .. module:: django.middleware.csrf
  123. :synopsis: Middleware adding protection against Cross Site Request
  124. Forgeries.
  125. .. class:: CsrfMiddleware
  126. Adds protection against Cross Site Request Forgeries by adding hidden form
  127. fields to POST forms and checking requests for the correct value. See the
  128. :doc:`Cross Site Request Forgery protection documentation </ref/contrib/csrf>`.
  129. Transaction middleware
  130. ----------------------
  131. .. module:: django.middleware.transaction
  132. :synopsis: Middleware binding a database transaction to each Web request.
  133. .. class:: TransactionMiddleware
  134. Binds commit and rollback to the request/response phase. If a view function
  135. runs successfully, a commit is done. If it fails with an exception, a rollback
  136. is done.
  137. The order of this middleware in the stack is important: middleware modules
  138. running outside of it run with commit-on-save - the default Django behavior.
  139. Middleware modules running inside it (coming later in the stack) will be under
  140. the same transaction control as the view functions.
  141. See the :doc:`transaction management documentation </topics/db/transactions>`.