tags:

views:

293

answers:

4

I'm working on a blog application, and I want to have a sidebar that includes a list of all the months the blog has been in existence, to provide links to archives pages. Moreover, I'd like to make this automatically update when the month changes, rather than hardcoding it in the template. Of course, as far as I can tell, this means that I'll have to calculate the list of months in every view, and pass it into every template from every view.

I'd like to avoid this, if possible. Is there a way to calculate the list once and automatically apply it to every template, without having to explicitly pass it into the template from every view?

+3  A: 

You want a template context processor

http://stackoverflow.com/questions/557460/django-having-middleware-communicate-with-views-templates

http://docs.djangoproject.com/en/dev/ref/templates/api/?from=olddocs#id1

S.Lott
You know too much. :)
fuentesjr
-1 for apparently confusing middleware and template context processors, which have nothing to do with one other.
Carl Meyer
@Carl Meyer: I think the distinction is artificial, but I stand corrected.
S.Lott
+5  A: 

There are a few possible solutions to your problem.

If you really want to have this on every page on your site a context processor is probably your best choice. Context processors are basic way to inject data into all template contexts. Be aware however that the context processor will be called on every request.

An alternative solution would be to create a custom template tag and use it on a shared base template for all of the pages you wish to have your sidebar. Template tags are a bit more complex to create but they are more flexible.

With either solution you should also look at Django's cache framework. The cache framework makes it pretty easy to temporarily store your calculated values for a while to save some work on each request.

SeanOC
+1 for mentioning a custom template tag. Unless this is a value you will use on literally every single page on the site, that's a better approach than a context processor.
Carl Meyer
A: 

Django's template inheritance should cover this. You could create a base template that handles your sidebar functionality. Your other views extend this template.

Template Inheritance: http://www.djangobook.com/en/1.0/chapter04/#s-template-inheritance

Harold
I already use template inheritance. How will that prevent me from having to pass the variable from each view into the template?
mipadi
Sorry, meant to add in the answer above that you can generate/modify the base template from an external script that runs on the first of the month.
Harold
A: 

A combination of custom template tags as mentioned previously and template fragment caching should do the trick.

andybak
+1 for fragment caching. Of course, you still have to get the data to the fragment should it be stale. We fixed the variable lookup in the template code to permit passing in functions. The function (curried so its params don't have to be passed) only gets called during fragment regeneration.
Peter Rowell