Quickstart

django-phased is an implementation of a two-phase template rendering system to allow caching of pages that otherwise would be uncachable due to user-specific content that needs to be rendered (such as a signed-in notice in the corner of the page). This technique has been described in detail by Adrian Holovaty in this blog post.

Installation

To install django-phased, either check out the source from Github or install from PyPI:

  • Check out django-phased from GitHub and run python setup.py install in the source checkout

    or

  • Run pip install django-phased.

Setup

To make django-phased tags available to your templates, add 'phased' to your INSTALLED_APPS.

You can either use phased via the PhasedRenderMiddleware middleware or the phasedcache template tag.

Usage

Middleware

Install the PhasedRenderMiddleware to enable second-phase rendering of templates.

If using Django’s caching middleware, use PatchedVaryUpdateCacheMiddleware to bypass the Vary: Cookie behavior of that middleware.

A common setup for middleware classes would be this:

MIDDLEWARE_CLASSES = (
    'phased.middleware.PhasedRenderMiddleware',
    'phased.middleware.PatchedVaryUpdateCacheMiddleware',
    ...
    'django.middleware.cache.FetchFromCacheMiddleware',
)

See Settings for additional settings.

Template Tag

In order to use the phasedcache template tag you need to add 'django.core.context_processors.request' to the TEMPLATE_CONTEXT_PROCESSORS settings variable and use RequestContext when you render your templates. See the Django docs on how to use RequestContext in your views.

The phasedcache template tag works exactly like Django’s cache template tag except that it will run a second render pass using the second_pass_render function with value returned from the cache.

See phasedcache for details.

Project Versions

Table Of Contents

Previous topic

django-phased

Next topic

Settings

This Page