Note that there are some explanatory texts on larger screens.

plurals
  1. POPractical rules for Django MiddleWare ordering?
    primarykey
    data
    text
    <p>The official documentation is a bit messy: 'before' &amp; 'after' are used for ordering MiddleWare in a tuple, but in some places 'before'&amp;'after' refers to request-response phases. Also, 'should be first/last' are mixed and it's not clear which one to use as 'first'.</p> <p>I do understand the difference.. however it seems to complicated for a newbie in Django.</p> <p>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?</p> <p>here's the list, with the info from docs I managed to find:</p> <ol> <li><code>UpdateCacheMiddleware</code> <ul> <li>Before those that modify 'Vary:' <code>SessionMiddleware</code>, <code>GZipMiddleware</code>, <code>LocaleMiddleware</code></li> </ul></li> <li><code>GZipMiddleware</code> <ul> <li>Before any MW that may change or use the response body</li> <li>After <code>UpdateCacheMiddleware</code>: Modifies 'Vary:'</li> </ul></li> <li><code>ConditionalGetMiddleware</code> <ul> <li>Before <code>CommonMiddleware</code>: uses its 'Etag:' header when <code>USE_ETAGS=True</code></li> </ul></li> <li><code>SessionMiddleware</code> <ul> <li>After <code>UpdateCacheMiddleware</code>: Modifies 'Vary:'</li> <li>Before <code>TransactionMiddleware</code>: we don't need transactions here</li> </ul></li> <li><code>LocaleMiddleware</code>, One of the topmost, after SessionMiddleware, CacheMiddleware <ul> <li>After <code>UpdateCacheMiddleware</code>: Modifies 'Vary:'</li> <li>After <code>SessionMiddleware</code>: uses session data</li> </ul></li> <li><code>CommonMiddleware</code> <ul> <li>Before any MW that may change the response (it calculates ETags)</li> <li>After <code>GZipMiddleware</code> so it won't calculate an E-Tag on gzipped contents</li> <li>Close to the top: it redirects when <code>APPEND_SLASH</code> or <code>PREPEND_WWW</code></li> </ul></li> <li><code>CsrfViewMiddleware</code> <ul> <li>Before any view middleware that assumes that CSRF attacks have been dealt with</li> </ul></li> <li><code>AuthenticationMiddleware</code> <ul> <li>After <code>SessionMiddleware</code>: uses session storage</li> </ul></li> <li><code>MessageMiddleware</code> <ul> <li>After <code>SessionMiddleware</code>: can use Session-based storage</li> </ul></li> <li><code>XViewMiddleware</code></li> <li><code>TransactionMiddleware</code> <ul> <li>After MWs that use DB: <code>SessionMiddleware</code> (configurable to use DB)</li> <li>All <code>*CacheMiddleWare</code> is not affected (as an exception: uses own DB cursor)</li> </ul></li> <li><code>FetchFromCacheMiddleware</code> <ul> <li>After those those that modify 'Vary:' if uses them to pick a value for cache hash-key</li> <li>After <code>AuthenticationMiddleware</code> so it's possible to use <code>CACHE_MIDDLEWARE_ANONYMOUS_ONLY</code></li> </ul></li> <li><code>FlatpageFallbackMiddleware</code> <ul> <li>Bottom: last resort</li> <li>Uses DB, however, is not a problem for <code>TransactionMiddleware</code> <strong>(yes?)</strong></li> </ul></li> <li><code>RedirectFallbackMiddleware</code> <ul> <li>Bottom: last resort</li> <li>Uses DB, however, is not a problem for <code>TransactionMiddleware</code> <strong>(yes?)</strong></li> </ul></li> </ol> <p>(I will add suggestions to this list to collect all of them in one place)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload