34
The official documentation is a bit messy: 'before' & 'after' are used for ordering MiddleWare in a tuple, but in some places 'before'&'after' refers to request-response phases. Also, 'should be first/last' are mixed and it's not clear which one to use as 'first'.
I do understand the difference.. however it seems to complicated for a newbie in Django.
Can you suggest some correct ordering for builtin MiddleWare classes (assuming we enable all of them) and — most importantly — explain WHY one goes before/after other ones?
here's the list, with the info from docs I managed to find:
UpdateCacheMiddleware- Before those that modify 'Vary:'
SessionMiddleware,GZipMiddleware,LocaleMiddleware
- Before those that modify 'Vary:'
GZipMiddleware- Before any MW that may change or use the response body
- After
UpdateCacheMiddleware: Modifies 'Vary:'
ConditionalGetMiddleware- Before
CommonMiddleware: uses its 'Etag:' header whenUSE_ETAGS=True
- Before
SessionMiddleware- After
UpdateCacheMiddleware: Modifies 'Vary:' - Before
TransactionMiddleware: we don't need transactions here
- After
LocaleMiddleware, One of the topmost, after SessionMiddleware, CacheMiddleware- After
UpdateCacheMiddleware: Modifies 'Vary:' - After
SessionMiddleware: uses session data
- After
CommonMiddleware- Before any MW that may change the response (it calculates ETags)
- After
GZipMiddlewareso it won't calculate an E-Tag on gzipped contents - Close to the top: it redirects when
APPEND_SLASHorPREPEND_WWW
CsrfViewMiddleware- Before any view middleware t