Django includes several built-in middleware applications, some of which you have already been using. You can install each one by adding it to the MIDDLEWARE_CLASSES setting in the settings.py file.
The following sections briefly discuss some of the built-in middleware applications. For more information about these and other built-in middleware applications, check out the source at the following location relative to the Django installation directory:
django/middleware
The CommonMiddleware application provides some basic functionality to the website. It forbids access to user agents that are listed in the DISALLOWED_USER_AGENTS setting of the settings.py file.
CommonMiddleware also allows you to implement the APPEND_SLASH and PREPEND_WWW settings to add trailing slashes to the end and prefix a www to URLs. These options help you normalize URLs to match expected behavior.
CommonMiddleware also allows you to set USE_ETAGS=True in the settings.py file. If USE_ETAGS is True, Django calculates an ETag for each request using an MD5 hashing of the page content.
The SessionMiddleware application enables session support in requests. It provides the request.session attribute that you have already been using to the request object that is passed to view functions.
The AuthenticationMiddleware application provides authentication support to requests. It provides the request.user attribute that you have already been using to the request object that is passed to view functions.
The CacheMiddleware application enables site-wide caching. You can set the CACHE_MIDDLEWARE_SECONDS setting in the settings.py file to define how long the Django web pages are cached.
The GZipMiddleware application allows you to reduce the impact of traffic by compressing content that is sent to browsers that support gzip compression.
By the Way
Django recommends that you put this in the first place in the MIDDLEWARE_CLASSES setting so that compression is the last thing that is performed in the postresponse handlers.
The LocaleMiddleware application allows you to select language based on the data provided in the request. Thus, the web content can be customized on a per-user basis.
Examples of how to implement the built-in middleware applications are contained in the sections of this book where they are used. For example, an example of implementing the caching middleware is located in Hour 23, "Configuring Caching."