class CommonMiddleware
[source]
Adds a few conveniences for perfectionists:
- Forbids access to user agents in the
DISALLOWED_USER_AGENTS
setting, which should be a list of compiled regular expression objects. -
Performs URL rewriting based on the
APPEND_SLASH
andPREPEND_WWW
settings.If
APPEND_SLASH
isTrue
and the initial URL doesn’t end with a slash, 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.For example,
foo.com/bar
will be redirected tofoo.com/bar/
if you don’t have a valid URL pattern forfoo.com/bar
but do have a valid pattern forfoo.com/bar/
.If
PREPEND_WWW
isTrue
, URLs that lack a leading “www.” will be redirected to the same URL with a leading “www.”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 fromfoo.com/bar/
– a search-engine indexer would treat them as separate URLs – so it’s best practice to normalize URLs. - Handles ETags based on the
USE_ETAGS
setting. IfUSE_ETAGS
is set toTrue
, Django will calculate an ETag for each request by MD5-hashing the page content, and it’ll take care of sendingNot Modified
responses, if appropriate.
Please login to continue.