Skip to main content

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

Project description

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](http://www.holovaty.com/writing/django-two-phased-rendering/) 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.

Documentation lives in the docs directory as Sphinx documentation or [in HTML rendered form here](http://codysoyland.com/projects/django-phased/documentation/).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-phased-0.5.tar.gz (8.4 kB view details)

Uploaded Source

File details

Details for the file django-phased-0.5.tar.gz.

File metadata

  • Download URL: django-phased-0.5.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for django-phased-0.5.tar.gz
Algorithm Hash digest
SHA256 524c84d08c4933c0b45959b68fc4731507e86df3c20e6706f43e9c733ff9db02
MD5 6450ac79215160a4a43bb476f73afe39
BLAKE2b-256 5962dc4fda7333de7929ebfe1f15a31a08ce0a60aa5c4dda1e56b6855ae74936

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page