Middleware ordering

Table 17.1 provides some hints about the ordering of various Django middleware classes:

Class

Notes

UpdateCacheMiddleware

Before those that modify the Vary header (SessionMiddleware, GZipMiddleware, LocaleMiddleware).

GZipMiddleware

Before any middleware that may change or use the response body.

After UpdateCacheMiddleware: Modifies Vary header.

ConditionalGetMiddleware

Before CommonMiddleware: uses its Etag header when USE_ETAGS = True.

SessionMiddleware

After UpdateCacheMiddleware: Modifies Vary header.

LocaleMiddleware

One of the topmost, after SessionMiddleware (uses session data) and CacheMiddleware (modifies Vary header).

CommonMiddleware

Before any middleware that may change the response (it calculates ...

Get Mastering Django: Core now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.