June 28, 2008

Writing Custom Contexts for Django

A context is just some information. They will usually be QuerySets, but they could be anything. They will be available from within your templates. Oh, and they are a breeze to make.

When building my site I wanted to have a selection of random entries available on the navigation bar on each page, lets look at how we can do this with a context.

Contexts are simply functions that take one argument, request, and return a dictionary of values. The one I wrote looks like this:

Link Posted by Tim at June 28, 2008 09:15 PM | TrackBack