django-phased provides two helpful middleware classes, PhasedRenderMiddleware and PatchedVaryUpdateCacheMiddleware.
Performs a second-phase template rendering on the response and should be placed before the UpdateCacheMiddleware (or PatchedVaryUpdateCacheMiddleware) in the MIDDLEWARE_CLASES setting.
If the content-type starts with text/html performs a second-phase render on response.content and updates the Content-Length header of the response to reflect the change in size after rendering.
If Vary: Cookie is set in the response object, Django’s cache middleware will vary the cache key based on the value of the cookie.
This subclass of Django’s UpdateCacheMiddleware is designed to cache without varying the cache key on cookie contents.
This removes the Vary: Cookie header prior to running the standard Django UpdateCacheMiddleware.process_response() and adds the header back after caching so that in-browser caches are aware to vary the cache on cookies.