Skip to main content

django-phased

Build Status

A simple two-phase template rendering application useful for caching of authenticated requests.

How it works

This technique has been desribed by Adrian Holovaty in this blog post and previously by Honza Kral. The idea is to first render the template with certain blocks denoted as “phased,” such that they will not be rendered, and will remain valid template code that can be rendered with a second pass.

The second pass fetches the partially-rendered template from the cache and performs a second render on it, using RequestContext to provide user-specific context to the template. This enables very fast generation of pages that have user-specific content, by bypassing the need to use the CACHE_MIDDLEWARE_ANONYMOUS_ONLY setting.

This implementation uses a secret delimiter that makes it safe against the possibility of template code injection vulnerabilities, as it only passes any given text through the template parser once. The phased blocks can also contain cached context.

Basic Implementation

django-phased contains a templatetag, phased, which defines blocks that are to be parsed during the second phase. A middleware class, PhasedRenderMiddleware, processes the response to render the parts that were skipped during the first rendering.

A special subclass of UpdateCacheMiddleware that drops the “Vary: Cookie” header from response when it updates the cache is also included, which, if used in place of the standard UpdateCacheMiddleware will prevent the cache middleware from varying the cache key based on cookies, thus enabling caching of pages in authenticated sessions.

Release files for django-phased 0.6.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-phased 0.6.1
File Size Uploaded
django-phased-0.6.1.tar.gz 11.3 kB Details

Release files / django-phased-0.6.1.tar.gz

Download URL django-phased-0.6.1.tar.gz
Size 11.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a48b3b314ceb18a5439df3b6ce6331dcf6635b2b77a54a0e823ae5d1b6dc84a4
BLAKE2b-256 checksum
How to use checksums
edcc19b2988e29588fd9aaa281c7e93b194f7f38bce5e725a011a08bb03c9d7c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.6.1 This release

1 release file

0.6

1 release file

0.5.1

1 release file

0.5

1 release file

0.4.0

1 release file

0.3.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page