Django Doctor audits code and auto fixes Django anti-patterns. We checked 666 Django projects for problems hindering maintainability and found that 8% of the Django websites have incorrectly ordered middleware read more.
Incorrect middleware order is can break middleware hence docs for middleware advise this should come before x or after y. Some middleware read headers that others set. Some set headers that affect caching. Some perform actions on the rendered response content such as gzip. Thus we can see why order is important.
Let's delve into the order the built-in Django middleware should conform to, and why your website might break if you don't adhere to this order. Incorrect ordered middleware can break make these features not work as you intended at times.
SecurityMiddleware A middleware that hardens security. Security checks should happen as early in the request as possible, so the advice is to have that at the start of the middleware. See here for our deep dive into this middleware and videos demonstrating the hacks your website is vulnerable to without it.
CommonMiddleware As the name implies, this performs common operations we expect Django to do such as redirecting according to
PREPEND_WWW setting. If Django is redirecting based on the URL, we want that to happen as soon as possible, hence this middlware comes at the start. The middleware also sets content-length, which other middlwares may rely on.
LocaleMiddleware This handles internationalization of content. It customizes content for each user. We would not want any part of our website to miss that language-specific and user-specific customization and have the header in English the footer in Spanish. no, muévelo cerca del principio.
There are two built in fallback middlewares that perform some operation if all else fails:
RedirectFallbackMiddleware This middleware handles 404s by redirecting somewhere according to redirect records set in Django admin. We would not want this to happen at the start otherwise database records would take precedence over code in urls.py, and also the database lookup would slow down the response that do not need a fallback.
And then the spider web begins:
Or try out Django refactor challenges.