mirror of https://github.com/django/django.git
A few minor wording, whitespace, punctuation, and link changes for the middleware documentation.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@9833 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
bfbd62752a
commit
f76cb41251
|
@ -8,8 +8,8 @@ Built-in middleware reference
|
||||||
:synopsis: Django's built-in middleware classes.
|
:synopsis: Django's built-in middleware classes.
|
||||||
|
|
||||||
This document explains all middleware components that come with Django. For
|
This document explains all middleware components that come with Django. For
|
||||||
information on how how to use them and how to write your own middleware, see the
|
information on how how to use them and how to write your own middleware, see
|
||||||
:ref:`middleware usage guide <topics-http-middleware>`.
|
the :ref:`middleware usage guide <topics-http-middleware>`.
|
||||||
|
|
||||||
Available middleware
|
Available middleware
|
||||||
====================
|
====================
|
||||||
|
@ -18,8 +18,8 @@ Cache middleware
|
||||||
----------------
|
----------------
|
||||||
|
|
||||||
.. module:: django.middleware.cache
|
.. module:: django.middleware.cache
|
||||||
:synopsis: Middleware for the site-wide cache
|
:synopsis: Middleware for the site-wide cache.
|
||||||
|
|
||||||
.. class:: django.middleware.cache.UpdateCacheMiddleware
|
.. class:: django.middleware.cache.UpdateCacheMiddleware
|
||||||
|
|
||||||
.. class:: django.middleware.cache.FetchFromCacheMiddleware
|
.. class:: django.middleware.cache.FetchFromCacheMiddleware
|
||||||
|
@ -33,7 +33,7 @@ defines. See the :ref:`cache documentation <topics-cache>`.
|
||||||
|
|
||||||
.. module:: django.middleware.common
|
.. module:: django.middleware.common
|
||||||
:synopsis: Middleware adding "common" conveniences for perfectionists.
|
:synopsis: Middleware adding "common" conveniences for perfectionists.
|
||||||
|
|
||||||
.. class:: django.middleware.common.CommonMiddleware
|
.. class:: django.middleware.common.CommonMiddleware
|
||||||
|
|
||||||
Adds a few conveniences for perfectionists:
|
Adds a few conveniences for perfectionists:
|
||||||
|
@ -45,14 +45,14 @@ Adds a few conveniences for perfectionists:
|
||||||
:setting:`PREPEND_WWW` settings.
|
:setting:`PREPEND_WWW` settings.
|
||||||
|
|
||||||
If :setting:`APPEND_SLASH` is ``True`` and the initial URL doesn't end
|
If :setting:`APPEND_SLASH` is ``True`` and the initial URL doesn't end
|
||||||
with a slash, and it is not found in the URLconf, then a new URL is formed
|
with a slash, and it is not found in the URLconf, then a new URL is
|
||||||
by appending a slash at the end. If this new URL is found in the URLconf,
|
formed by appending a slash at the end. If this new URL is found in the
|
||||||
then Django redirects the request to this new URL. Otherwise, the initial
|
URLconf, then Django redirects the request to this new URL. Otherwise,
|
||||||
URL is processed as usual.
|
the initial URL is processed as usual.
|
||||||
|
|
||||||
For example, ``foo.com/bar`` will be redirected to ``foo.com/bar/`` if you
|
For example, ``foo.com/bar`` will be redirected to ``foo.com/bar/`` if
|
||||||
don't have a valid URL pattern for ``foo.com/bar`` but *do* have a valid
|
you don't have a valid URL pattern for ``foo.com/bar`` but *do* have a
|
||||||
pattern for ``foo.com/bar/``.
|
valid pattern for ``foo.com/bar/``.
|
||||||
|
|
||||||
.. versionchanged:: 1.0
|
.. versionchanged:: 1.0
|
||||||
The behavior of :setting:`APPEND_SLASH` has changed slightly in this
|
The behavior of :setting:`APPEND_SLASH` has changed slightly in this
|
||||||
|
@ -69,8 +69,8 @@ Adds a few conveniences for perfectionists:
|
||||||
normalize URLs.
|
normalize URLs.
|
||||||
|
|
||||||
* Handles ETags based on the :setting:`USE_ETAGS` setting. If
|
* Handles ETags based on the :setting:`USE_ETAGS` setting. If
|
||||||
:setting:`USE_ETAGS` is set to ``True``, Django will calculate an ETag for
|
:setting:`USE_ETAGS` is set to ``True``, Django will calculate an ETag
|
||||||
each request by MD5-hashing the page content, and it'll take care of
|
for each request by MD5-hashing the page content, and it'll take care of
|
||||||
sending ``Not Modified`` responses, if appropriate.
|
sending ``Not Modified`` responses, if appropriate.
|
||||||
|
|
||||||
View metadata middleware
|
View metadata middleware
|
||||||
|
@ -90,7 +90,7 @@ GZIP middleware
|
||||||
|
|
||||||
.. module:: django.middleware.gzip
|
.. module:: django.middleware.gzip
|
||||||
:synopsis: Middleware to serve gziped content for performance.
|
:synopsis: Middleware to serve gziped content for performance.
|
||||||
|
|
||||||
.. class:: django.middleware.gzip.GZipMiddleware
|
.. class:: django.middleware.gzip.GZipMiddleware
|
||||||
|
|
||||||
Compresses content for browsers that understand gzip compression (all modern
|
Compresses content for browsers that understand gzip compression (all modern
|
||||||
|
@ -139,11 +139,12 @@ Locale middleware
|
||||||
|
|
||||||
.. module:: django.middleware.locale
|
.. module:: django.middleware.locale
|
||||||
:synopsis: Middleware to enable language selection based on the request.
|
:synopsis: Middleware to enable language selection based on the request.
|
||||||
|
|
||||||
.. class:: django.middleware.locale.LocaleMiddleware
|
.. class:: django.middleware.locale.LocaleMiddleware
|
||||||
|
|
||||||
Enables language selection based on data from the request. It customizes content
|
Enables language selection based on data from the request. It customizes
|
||||||
for each user. See the :ref:`internationalization documentation <topics-i18n>`.
|
content for each user. See the :ref:`internationalization documentation
|
||||||
|
<topics-i18n>`.
|
||||||
|
|
||||||
Session middleware
|
Session middleware
|
||||||
------------------
|
------------------
|
||||||
|
@ -160,18 +161,20 @@ Authentication middleware
|
||||||
-------------------------
|
-------------------------
|
||||||
|
|
||||||
.. module:: django.contrib.auth.middleware
|
.. module:: django.contrib.auth.middleware
|
||||||
:synopsis: Authentication middleware
|
:synopsis: Authentication middleware.
|
||||||
|
|
||||||
.. class:: django.contrib.auth.middleware.AuthenticationMiddleware
|
.. class:: django.contrib.auth.middleware.AuthenticationMiddleware
|
||||||
|
|
||||||
Adds the ``user`` attribute, representing the currently-logged-in user, to every
|
Adds the ``user`` attribute, representing the currently-logged-in user, to
|
||||||
incoming ``HttpRequest`` object. See :ref:`Authentication in Web requests <topics-auth>`.
|
every incoming ``HttpRequest`` object. See :ref:`Authentication in Web requests
|
||||||
|
<topics-auth>`.
|
||||||
|
|
||||||
CSRF protection middleware
|
CSRF protection middleware
|
||||||
--------------------------
|
--------------------------
|
||||||
|
|
||||||
.. module:: django.contrib.csrf.middleware
|
.. module:: django.contrib.csrf.middleware
|
||||||
:synopsis: Middleware adding protection against Cross Site Request Forgeries.
|
:synopsis: Middleware adding protection against Cross Site Request
|
||||||
|
Forgeries.
|
||||||
|
|
||||||
.. class:: django.contrib.csrf.middleware.CsrfMiddleware
|
.. class:: django.contrib.csrf.middleware.CsrfMiddleware
|
||||||
|
|
||||||
|
@ -189,9 +192,9 @@ Transaction middleware
|
||||||
|
|
||||||
.. class:: django.middleware.transaction.TransactionMiddleware
|
.. class:: django.middleware.transaction.TransactionMiddleware
|
||||||
|
|
||||||
Binds commit and rollback to the request/response phase. If a view function runs
|
Binds commit and rollback to the request/response phase. If a view function
|
||||||
successfully, a commit is done. If it fails with an exception, a rollback is
|
runs successfully, a commit is done. If it fails with an exception, a rollback
|
||||||
done.
|
is done.
|
||||||
|
|
||||||
The order of this middleware in the stack is important: middleware modules
|
The order of this middleware in the stack is important: middleware modules
|
||||||
running outside of it run with commit-on-save - the default Django behavior.
|
running outside of it run with commit-on-save - the default Django behavior.
|
||||||
|
@ -199,4 +202,3 @@ Middleware modules running inside it (coming later in the stack) will be under
|
||||||
the same transaction control as the view functions.
|
the same transaction control as the view functions.
|
||||||
|
|
||||||
See the :ref:`transaction management documentation <topics-db-transactions>`.
|
See the :ref:`transaction management documentation <topics-db-transactions>`.
|
||||||
|
|
||||||
|
|
|
@ -13,9 +13,9 @@ example, Django includes a middleware component, ``XViewMiddleware``, that adds
|
||||||
an ``"X-View"`` HTTP header to every response to a ``HEAD`` request.
|
an ``"X-View"`` HTTP header to every response to a ``HEAD`` request.
|
||||||
|
|
||||||
This document explains how middleware works, how you activate middleware, and
|
This document explains how middleware works, how you activate middleware, and
|
||||||
how to write your own middleware. Django ships with some built-in middleware you
|
how to write your own middleware. Django ships with some built-in middleware
|
||||||
can use right out of the box; they're documented in the :ref:`built-in
|
you can use right out of the box; they're documented in the :ref:`built-in
|
||||||
middleware guide <ref-middleware>`.
|
middleware reference <ref-middleware>`.
|
||||||
|
|
||||||
Activating middleware
|
Activating middleware
|
||||||
=====================
|
=====================
|
||||||
|
@ -36,9 +36,9 @@ created by :djadmin:`django-admin.py startproject <startproject>`::
|
||||||
During the request phases (:meth:`process_request` and :meth:`process_view`
|
During the request phases (:meth:`process_request` and :meth:`process_view`
|
||||||
middleware), Django applies middleware in the order it's defined in
|
middleware), Django applies middleware in the order it's defined in
|
||||||
:setting:`MIDDLEWARE_CLASSES`, top-down. During the response phases
|
:setting:`MIDDLEWARE_CLASSES`, top-down. During the response phases
|
||||||
(:meth:`process_response` and :meth:`process_exception` middleware), the classes
|
(:meth:`process_response` and :meth:`process_exception` middleware), the
|
||||||
are applied in reverse order, from the bottom up. You can think of it like an
|
classes are applied in reverse order, from the bottom up. You can think of it
|
||||||
onion: each middleware class is a "layer" that wraps the view:
|
like an onion: each middleware class is a "layer" that wraps the view:
|
||||||
|
|
||||||
.. image:: _images/middleware.png
|
.. image:: _images/middleware.png
|
||||||
:width: 502
|
:width: 502
|
||||||
|
@ -81,21 +81,22 @@ Response middleware is always called on every response.
|
||||||
|
|
||||||
.. method:: process_view(self, request, view_func, view_args, view_kwargs)
|
.. method:: process_view(self, request, view_func, view_args, view_kwargs)
|
||||||
|
|
||||||
``request`` is an :class:`~django.http.HttpRequest` object. ``view_func`` is the
|
``request`` is an :class:`~django.http.HttpRequest` object. ``view_func`` is
|
||||||
Python function that Django is about to use. (It's the actual function object,
|
the Python function that Django is about to use. (It's the actual function
|
||||||
not the name of the function as a string.) ``view_args`` is a list of positional
|
object, not the name of the function as a string.) ``view_args`` is a list of
|
||||||
arguments that will be passed to the view, and ``view_kwargs`` is a dictionary
|
positional arguments that will be passed to the view, and ``view_kwargs`` is a
|
||||||
of keyword arguments that will be passed to the view. Neither ``view_args`` nor
|
dictionary of keyword arguments that will be passed to the view. Neither
|
||||||
``view_kwargs`` include the first view argument (``request``).
|
``view_args`` nor ``view_kwargs`` include the first view argument
|
||||||
|
(``request``).
|
||||||
|
|
||||||
``process_view()`` is called just before Django calls the view. It should return
|
``process_view()`` is called just before Django calls the view. It should
|
||||||
either ``None`` or an :class:`~django.http. HttpResponse` object. If it returns
|
return either ``None`` or an :class:`~django.http. HttpResponse` object. If it
|
||||||
``None``, Django will continue processing this request, executing any other
|
returns ``None``, Django will continue processing this request, executing any
|
||||||
``process_view()`` middleware and, then, the appropriate view. If it returns an
|
other ``process_view()`` middleware and, then, the appropriate view. If it
|
||||||
:class:`~django.http. HttpResponse` object, Django won't bother calling ANY
|
returns an :class:`~django.http. HttpResponse` object, Django won't bother
|
||||||
other request, view or exception middleware, or the appropriate view; it'll
|
calling ANY other request, view or exception middleware, or the appropriate
|
||||||
return that :class:`~django.http. HttpResponse`. Response middleware is always
|
view; it'll return that :class:`~django.http. HttpResponse`. Response
|
||||||
called on every response.
|
middleware is always called on every response.
|
||||||
|
|
||||||
.. _response-middleware:
|
.. _response-middleware:
|
||||||
|
|
||||||
|
@ -124,8 +125,8 @@ brand-new :class:`~django.http. HttpResponse`.
|
||||||
Django calls ``process_exception()`` when a view raises an exception.
|
Django calls ``process_exception()`` when a view raises an exception.
|
||||||
``process_exception()`` should return either ``None`` or an
|
``process_exception()`` should return either ``None`` or an
|
||||||
:class:`~django.http. HttpResponse` object. If it returns an
|
:class:`~django.http. HttpResponse` object. If it returns an
|
||||||
:class:`~django.http. HttpResponse` object, the response will be returned to the
|
:class:`~django.http. HttpResponse` object, the response will be returned to
|
||||||
browser. Otherwise, default exception handling kicks in.
|
the browser. Otherwise, default exception handling kicks in.
|
||||||
|
|
||||||
``__init__``
|
``__init__``
|
||||||
------------
|
------------
|
||||||
|
@ -137,7 +138,7 @@ of caveats:
|
||||||
|
|
||||||
* Django initializes your middleware without any arguments, so you can't
|
* Django initializes your middleware without any arguments, so you can't
|
||||||
define ``__init__`` as requiring any arguments.
|
define ``__init__`` as requiring any arguments.
|
||||||
|
|
||||||
* Unlike the ``process_*`` methods which get called once per request,
|
* Unlike the ``process_*`` methods which get called once per request,
|
||||||
``__init__`` gets called only *once*, when the web server starts up.
|
``__init__`` gets called only *once*, when the web server starts up.
|
||||||
|
|
||||||
|
@ -146,8 +147,8 @@ Marking middleware as unused
|
||||||
|
|
||||||
It's sometimes useful to determine at run-time whether a piece of middleware
|
It's sometimes useful to determine at run-time whether a piece of middleware
|
||||||
should be used. In these cases, your middleware's ``__init__`` method may raise
|
should be used. In these cases, your middleware's ``__init__`` method may raise
|
||||||
``django.core.exceptions.MiddlewareNotUsed``. Django will then remove that piece
|
``django.core.exceptions.MiddlewareNotUsed``. Django will then remove that
|
||||||
of middleware from the middleware process.
|
piece of middleware from the middleware process.
|
||||||
|
|
||||||
Guidelines
|
Guidelines
|
||||||
----------
|
----------
|
||||||
|
@ -155,14 +156,11 @@ Guidelines
|
||||||
* Middleware classes don't have to subclass anything.
|
* Middleware classes don't have to subclass anything.
|
||||||
|
|
||||||
* The middleware class can live anywhere on your Python path. All Django
|
* The middleware class can live anywhere on your Python path. All Django
|
||||||
cares about is that the :setting:`MIDDLEWARE_CLASSES` setting includes the
|
cares about is that the :setting:`MIDDLEWARE_CLASSES` setting includes
|
||||||
path
|
the path to it.
|
||||||
to it.
|
|
||||||
|
|
||||||
* Feel free to look at :mod:`Django's available middleware for examples
|
* Feel free to look at :ref:`Django's available middleware
|
||||||
<django.middleware>`. The core Django middleware classes are in
|
<ref-middleware>` for examples.
|
||||||
``django/middleware/`` in the Django distribution. The session middleware
|
|
||||||
is in ``django/contrib/sessions``.
|
|
||||||
|
|
||||||
* If you write a middleware component that you think would be useful to
|
* If you write a middleware component that you think would be useful to
|
||||||
other people, contribute to the community! :ref:`Let us know
|
other people, contribute to the community! :ref:`Let us know
|
||||||
|
|
Loading…
Reference in New Issue