Like a good little coder, all of my Django templates inherit from a base.html. Now I would like to add some functionality to the base to always show some interesting things. Some user statistics, or random posts, or feeds, etc.
All of my views look like this:
def viewname(request) :
template_vales = {}
// Stuff
return render_to_response('some_file_name.html', template_values)
How can I make it so that the values of template_values are always populated for all my views? Do I have to do this at the start of all of my views? As in:
import utils
def viewname(request) :
template_values = {}
utils.addDefaults(template_values)
// Stuff
return render_to_response('some_file_name.html', template_values)
Or is there a better way?
You should use context processors:
http://docs.djangoproject.com/en/dev/ref/templates/api/
http://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/
In my settings.py, I add a couple of functions to the standard ones (see the last two):
The first one I add defines the canonical URL for the view, and the second switches between GMap keys. Here’s that function: