2005-08-31 20:24:50 +00:00
|
|
|
==========
|
|
|
|
Middleware
|
|
|
|
==========
|
|
|
|
|
|
|
|
Middleware is a framework of hooks into Django's request/response processing.
|
|
|
|
It's a light, low-level "plugin" system for globally altering Django's input
|
|
|
|
and/or output.
|
|
|
|
|
|
|
|
Each middleware component is responsible for doing some specific function. For
|
|
|
|
example, Django includes a middleware component, ``XViewMiddleware``, that adds
|
|
|
|
an ``"X-View"`` HTTP header to every response to a ``HEAD`` request.
|
|
|
|
|
|
|
|
This document explains all middleware components that come with Django, how to
|
|
|
|
use them, and how to write your own middleware.
|
|
|
|
|
|
|
|
Activating middleware
|
|
|
|
=====================
|
|
|
|
|
|
|
|
To activate a middleware component, add it to the ``MIDDLEWARE_CLASSES`` list
|
|
|
|
in your Django settings. In ``MIDDLEWARE_CLASSES``, each middleware component
|
|
|
|
is represented by a string: the full Python path to the middleware's class
|
|
|
|
name. For example, here's the default ``MIDDLEWARE_CLASSES`` created by
|
|
|
|
``django-admin.py startproject``::
|
|
|
|
|
|
|
|
MIDDLEWARE_CLASSES = (
|
2006-05-26 04:05:02 +00:00
|
|
|
'django.middleware.common.CommonMiddleware',
|
|
|
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
|
|
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
|
|
|
'django.middleware.doc.XViewMiddleware',
|
2005-08-31 20:24:50 +00:00
|
|
|
)
|
|
|
|
|
2005-11-11 00:25:03 +00:00
|
|
|
Django applies middleware in the order it's defined in ``MIDDLEWARE_CLASSES``,
|
2005-11-11 01:14:02 +00:00
|
|
|
except in the case of response and exception middleware, which is applied in
|
|
|
|
reverse order.
|
2005-08-31 20:24:50 +00:00
|
|
|
|
2005-10-19 01:09:05 +00:00
|
|
|
A Django installation doesn't require any middleware -- e.g.,
|
|
|
|
``MIDDLEWARE_CLASSES`` can be empty, if you'd like -- but it's strongly
|
|
|
|
suggested that you use ``CommonMiddleware``.
|
2005-08-31 20:24:50 +00:00
|
|
|
|
|
|
|
Available middleware
|
|
|
|
====================
|
|
|
|
|
2005-10-09 01:08:58 +00:00
|
|
|
django.middleware.cache.CacheMiddleware
|
|
|
|
---------------------------------------
|
2005-08-31 20:24:50 +00:00
|
|
|
|
2005-10-09 01:08:58 +00:00
|
|
|
Enables site-wide cache. If this is enabled, each Django-powered page will be
|
|
|
|
cached for as long as the ``CACHE_MIDDLEWARE_SECONDS`` setting defines. See
|
|
|
|
the `cache documentation`_.
|
2005-08-31 20:24:50 +00:00
|
|
|
|
2007-01-24 20:08:47 +00:00
|
|
|
.. _`cache documentation`: ../cache/#the-per-site-cache
|
2005-08-31 20:24:50 +00:00
|
|
|
|
2005-10-09 01:08:58 +00:00
|
|
|
django.middleware.common.CommonMiddleware
|
|
|
|
-----------------------------------------
|
2005-08-31 20:24:50 +00:00
|
|
|
|
2005-10-09 01:08:58 +00:00
|
|
|
Adds a few conveniences for perfectionists:
|
2005-08-31 20:24:50 +00:00
|
|
|
|
2005-10-09 01:08:58 +00:00
|
|
|
* Forbids access to user agents in the ``DISALLOWED_USER_AGENTS`` setting,
|
|
|
|
which should be a list of strings.
|
2005-08-31 20:24:50 +00:00
|
|
|
|
2005-10-09 01:08:58 +00:00
|
|
|
* Performs URL rewriting based on the ``APPEND_SLASH`` and ``PREPEND_WWW``
|
2007-12-02 23:25:55 +00:00
|
|
|
settings.
|
|
|
|
|
|
|
|
If ``APPEND_SLASH`` is ``True`` and the initial URL doesn't end with a slash,
|
2007-12-19 03:55:10 +00:00
|
|
|
and it is not found in the URLconf, then a new URL is formed by appending a
|
|
|
|
slash at the end. If this new URL is found in the URLconf, then Django
|
|
|
|
redirects the request to this new URL. Otherwise, the initial URL is
|
|
|
|
processed as usual.
|
2007-12-02 23:25:55 +00:00
|
|
|
|
2007-12-19 03:55:10 +00:00
|
|
|
For example, ``foo.com/bar`` will be redirected to ``foo.com/bar/`` if you
|
|
|
|
don't have a valid URL pattern for ``foo.com/bar`` but *do* have a valid
|
|
|
|
pattern for ``foo.com/bar/``.
|
2007-12-02 23:25:55 +00:00
|
|
|
|
2007-12-04 06:01:00 +00:00
|
|
|
**New in Django development version:** The behavior of ``APPEND_SLASH`` has
|
2007-12-19 03:55:10 +00:00
|
|
|
changed slightly in the development version. It didn't used to check whether
|
|
|
|
the pattern was matched in the URLconf.
|
2006-08-18 03:12:36 +00:00
|
|
|
|
2006-05-20 03:35:25 +00:00
|
|
|
If ``PREPEND_WWW`` is ``True``, URLs that lack a leading "www." will be
|
2005-10-09 01:08:58 +00:00
|
|
|
redirected to the same URL with a leading "www."
|
2005-08-31 20:24:50 +00:00
|
|
|
|
2005-10-09 01:08:58 +00:00
|
|
|
Both of these options are meant to normalize URLs. The philosophy is that
|
|
|
|
each URL should exist in one, and only one, place. Technically a URL
|
|
|
|
``foo.com/bar`` is distinct from ``foo.com/bar/`` -- a search-engine
|
|
|
|
indexer would treat them as separate URLs -- so it's best practice to
|
|
|
|
normalize URLs.
|
2005-08-31 20:24:50 +00:00
|
|
|
|
2005-10-09 01:08:58 +00:00
|
|
|
* Handles ETags based on the ``USE_ETAGS`` setting. If ``USE_ETAGS`` is set
|
|
|
|
to ``True``, Django will calculate an ETag for each request by
|
|
|
|
MD5-hashing the page content, and it'll take care of sending
|
|
|
|
``Not Modified`` responses, if appropriate.
|
2005-10-09 00:55:08 +00:00
|
|
|
|
2005-10-09 01:08:58 +00:00
|
|
|
django.middleware.doc.XViewMiddleware
|
|
|
|
-------------------------------------
|
2005-10-09 00:55:08 +00:00
|
|
|
|
2005-10-09 01:08:58 +00:00
|
|
|
Sends custom ``X-View`` HTTP headers to HEAD requests that come from IP
|
|
|
|
addresses defined in the ``INTERNAL_IPS`` setting. This is used by Django's
|
|
|
|
automatic documentation system.
|
2005-08-31 20:24:50 +00:00
|
|
|
|
2005-10-09 01:08:58 +00:00
|
|
|
django.middleware.gzip.GZipMiddleware
|
|
|
|
-------------------------------------
|
|
|
|
|
|
|
|
Compresses content for browsers that understand gzip compression (all modern
|
|
|
|
browsers).
|
|
|
|
|
2007-08-12 12:29:25 +00:00
|
|
|
It is suggested to place this first in the middleware list, so that the
|
|
|
|
compression of the response content is the last thing that happens. Will not
|
|
|
|
compress content bodies less than 200 bytes long, when the response code is
|
2008-07-27 18:34:21 +00:00
|
|
|
something other than 200, JavaScript files (for IE compatibitility), or
|
2007-08-12 12:29:25 +00:00
|
|
|
responses that have the ``Content-Encoding`` header already specified.
|
|
|
|
|
2005-10-09 01:08:58 +00:00
|
|
|
django.middleware.http.ConditionalGetMiddleware
|
|
|
|
-----------------------------------------------
|
|
|
|
|
|
|
|
Handles conditional GET operations. If the response has a ``ETag`` or
|
|
|
|
``Last-Modified`` header, and the request has ``If-None-Match`` or
|
|
|
|
``If-Modified-Since``, the response is replaced by an HttpNotModified.
|
|
|
|
|
2007-11-15 09:26:57 +00:00
|
|
|
Also sets the ``Date`` and ``Content-Length`` response-headers.
|
2005-10-09 01:08:58 +00:00
|
|
|
|
2006-08-18 03:12:36 +00:00
|
|
|
django.middleware.http.SetRemoteAddrFromForwardedFor
|
|
|
|
----------------------------------------------------
|
|
|
|
|
2007-01-03 22:40:06 +00:00
|
|
|
Sets ``request.META['REMOTE_ADDR']`` based on
|
|
|
|
``request.META['HTTP_X_FORWARDED_FOR']``, if the latter is set. This is useful
|
|
|
|
if you're sitting behind a reverse proxy that causes each request's
|
|
|
|
``REMOTE_ADDR`` to be set to ``127.0.0.1``.
|
2006-08-18 03:12:36 +00:00
|
|
|
|
|
|
|
**Important note:** This does NOT validate ``HTTP_X_FORWARDED_FOR``. If you're
|
|
|
|
not behind a reverse proxy that sets ``HTTP_X_FORWARDED_FOR`` automatically, do
|
|
|
|
not use this middleware. Anybody can spoof the value of
|
|
|
|
``HTTP_X_FORWARDED_FOR``, and because this sets ``REMOTE_ADDR`` based on
|
|
|
|
``HTTP_X_FORWARDED_FOR``, that means anybody can "fake" their IP address. Only
|
|
|
|
use this when you can absolutely trust the value of ``HTTP_X_FORWARDED_FOR``.
|
|
|
|
|
2007-07-12 13:41:27 +00:00
|
|
|
django.middleware.locale.LocaleMiddleware
|
|
|
|
-----------------------------------------
|
|
|
|
|
|
|
|
Enables language selection based on data from the request. It customizes content
|
|
|
|
for each user. See the `internationalization documentation`_.
|
|
|
|
|
|
|
|
.. _`internationalization documentation`: ../i18n/
|
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
django.contrib.sessions.middleware.SessionMiddleware
|
|
|
|
----------------------------------------------------
|
2005-10-09 01:08:58 +00:00
|
|
|
|
|
|
|
Enables session support. See the `session documentation`_.
|
|
|
|
|
2007-01-24 20:08:47 +00:00
|
|
|
.. _`session documentation`: ../sessions/
|
2005-08-31 20:24:50 +00:00
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
django.contrib.auth.middleware.AuthenticationMiddleware
|
|
|
|
-------------------------------------------------------
|
|
|
|
|
|
|
|
Adds the ``user`` attribute, representing the currently-logged-in user, to
|
|
|
|
every incoming ``HttpRequest`` object. See `Authentication in Web requests`_.
|
|
|
|
|
2007-01-24 20:08:47 +00:00
|
|
|
.. _Authentication in Web requests: ../authentication/#authentication-in-web-requests
|
2006-05-02 01:31:56 +00:00
|
|
|
|
2008-01-02 05:13:46 +00:00
|
|
|
django.contrib.csrf.middleware.CsrfMiddleware
|
|
|
|
---------------------------------------------
|
|
|
|
|
|
|
|
**New in Django development version**
|
|
|
|
|
|
|
|
Adds protection against Cross Site Request Forgeries by adding hidden form
|
|
|
|
fields to POST forms and checking requests for the correct value. See the
|
|
|
|
`Cross Site Request Forgery protection documentation`_.
|
|
|
|
|
|
|
|
.. _`Cross Site Request Forgery protection documentation`: ../csrf/
|
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
django.middleware.transaction.TransactionMiddleware
|
|
|
|
---------------------------------------------------
|
|
|
|
|
|
|
|
Binds commit and rollback to the request/response phase. If a view function runs
|
|
|
|
successfully, a commit is done. If it fails with an exception, a rollback is
|
|
|
|
done.
|
|
|
|
|
|
|
|
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.
|
|
|
|
Middleware modules running inside it (coming later in the stack) will be under
|
|
|
|
the same transaction control as the view functions.
|
|
|
|
|
|
|
|
See the `transaction management documentation`_.
|
|
|
|
|
2007-01-24 20:08:47 +00:00
|
|
|
.. _`transaction management documentation`: ../transactions/
|
2006-05-02 01:31:56 +00:00
|
|
|
|
2005-08-31 20:24:50 +00:00
|
|
|
Writing your own middleware
|
|
|
|
===========================
|
|
|
|
|
|
|
|
Writing your own middleware is easy. Each middleware component is a single
|
|
|
|
Python class that defines one or more of the following methods:
|
|
|
|
|
|
|
|
process_request
|
|
|
|
---------------
|
|
|
|
|
|
|
|
Interface: ``process_request(self, request)``
|
|
|
|
|
|
|
|
``request`` is an ``HttpRequest`` object. This method is called on each
|
|
|
|
request, before Django decides which view to execute.
|
|
|
|
|
|
|
|
``process_request()`` should return either ``None`` or an ``HttpResponse``
|
|
|
|
object. If it returns ``None``, Django will continue processing this request,
|
|
|
|
executing any other middleware and, then, the appropriate view. If it returns
|
2007-10-21 12:24:37 +00:00
|
|
|
an ``HttpResponse`` object, Django won't bother calling ANY other request,
|
|
|
|
view or exception middleware, or the appropriate view; it'll return that
|
|
|
|
``HttpResponse``. Response middleware is always called on every response.
|
2005-08-31 20:24:50 +00:00
|
|
|
|
|
|
|
process_view
|
|
|
|
------------
|
|
|
|
|
2005-11-27 23:03:56 +00:00
|
|
|
Interface: ``process_view(self, request, view_func, view_args, view_kwargs)``
|
2005-08-31 20:24:50 +00:00
|
|
|
|
|
|
|
``request`` is an ``HttpRequest`` object. ``view_func`` is the Python function
|
|
|
|
that Django is about to use. (It's the actual function object, not the name of
|
2005-11-27 23:03:56 +00:00
|
|
|
the function as a string.) ``view_args`` is a list of positional arguments that
|
|
|
|
will be passed to the view, and ``view_kwargs`` is a dictionary of keyword
|
|
|
|
arguments that will be passed to the view. Neither ``view_args`` nor
|
|
|
|
``view_kwargs`` include the first view argument (``request``).
|
2005-08-31 20:24:50 +00:00
|
|
|
|
|
|
|
``process_view()`` is called just before Django calls the view. It should
|
|
|
|
return either ``None`` or an ``HttpResponse`` object. If it returns ``None``,
|
|
|
|
Django will continue processing this request, executing any other
|
|
|
|
``process_view()`` middleware and, then, the appropriate view. If it returns an
|
2007-10-21 12:24:37 +00:00
|
|
|
``HttpResponse`` object, Django won't bother calling ANY other request, view
|
|
|
|
or exception middleware, or the appropriate view; it'll return that
|
|
|
|
``HttpResponse``. Response middleware is always called on every response.
|
2005-08-31 20:24:50 +00:00
|
|
|
|
2005-08-31 20:55:16 +00:00
|
|
|
process_response
|
|
|
|
----------------
|
|
|
|
|
|
|
|
Interface: ``process_response(self, request, response)``
|
|
|
|
|
|
|
|
``request`` is an ``HttpRequest`` object. ``response`` is the ``HttpResponse``
|
|
|
|
object returned by a Django view.
|
|
|
|
|
|
|
|
``process_response()`` should return an ``HttpResponse`` object. It could alter
|
|
|
|
the given ``response``, or it could create and return a brand-new
|
|
|
|
``HttpResponse``.
|
|
|
|
|
2005-10-15 02:20:35 +00:00
|
|
|
process_exception
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
Interface: ``process_exception(self, request, exception)``
|
|
|
|
|
|
|
|
``request`` is an ``HttpRequest`` object. ``exception`` is an ``Exception``
|
|
|
|
object raised by the view function.
|
|
|
|
|
|
|
|
Django calls ``process_exception()`` when a view raises an exception.
|
|
|
|
``process_exception()`` should return either ``None`` or an ``HttpResponse``
|
|
|
|
object. If it returns an ``HttpResponse`` object, the response will be returned
|
|
|
|
to the browser. Otherwise, default exception handling kicks in.
|
|
|
|
|
2005-08-31 20:24:50 +00:00
|
|
|
Guidelines
|
|
|
|
----------
|
|
|
|
|
|
|
|
* Middleware classes don't have to subclass anything.
|
|
|
|
|
|
|
|
* The middleware class can live anywhere on your Python path. All Django
|
|
|
|
cares about is that the ``MIDDLEWARE_CLASSES`` setting includes the path
|
|
|
|
to it.
|
|
|
|
|
|
|
|
* Feel free to look at Django's available middleware for examples. The
|
2006-05-02 01:31:56 +00:00
|
|
|
core Django middleware classes are in ``django/middleware/`` in the
|
2007-10-21 12:24:37 +00:00
|
|
|
Django distribution. The session middleware is in
|
|
|
|
``django/contrib/sessions``.
|
2005-08-31 20:24:50 +00:00
|
|
|
|
|
|
|
* If you write a middleware component that you think would be useful to
|
|
|
|
other people, contribute to the community! Let us know, and we'll
|
|
|
|
consider adding it to Django.
|