django-phased provides two helpful middleware classes, PhasedRenderMiddleware and PatchedVaryUpdateCacheMiddleware.

class phased.middleware.PhasedRenderMiddleware

Performs a second-phase template rendering on the response and should be placed before the UpdateCacheMiddleware (or PatchedVaryUpdateCacheMiddleware) in the MIDDLEWARE_CLASES setting.

process_response(request, response)

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.

class phased.middleware.PatchedVaryUpdateCacheMiddleware

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.

process_response(request, response)

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.

Project Versions

Previous topic


Next topic

Template tag

This Page