Middleware order is important hence docs for third party libraries advise this should come before x or after y. Some middleware read headers that others set. Some set headers that affect caching. Some perform some action on the rendered response content such as gzip.
Over a long-lived codebase it’s easy for middlware to fall out of order. During my time reviewing pull requests I give this feedback:
Below is the order the built-in Django middlware should conform to.
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 German. Nien! Hence this comes early.
SecurityMiddleware Another well named middleware that hardens security. We would not want security to be handled near the end of the request.
There are two built in fallback middlwares 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.
FlatpageFallbackMiddleware Very similar to
RedirectFallbackMiddleware but it serves flat pages instead of redirecting the user somewhere.
And then the spider web begins:
Or try out Django refactor challenges.