class CommonMiddleware [source]
Adds a few conveniences for perfectionists:
- Forbids access to user agents in the
DISALLOWED_USER_AGENTSsetting, which should be a list of compiled regular expression objects. -
Performs URL rewriting based on the
APPEND_SLASHandPREPEND_WWWsettings.If
APPEND_SLASHisTrueand 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/barwill be redirected tofoo.com/bar/if you don’t have a valid URL pattern forfoo.com/barbut do have a valid pattern forfoo.com/bar/.If
PREPEND_WWWisTrue, 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/baris 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_ETAGSsetting. IfUSE_ETAGSis set toTrue, Django will calculate an ETag for each request by MD5-hashing the page content, and it’ll take care of sendingNot Modifiedresponses, if appropriate.
Please login to continue.