Toast Driven

← Back to November 4, 2008


Short and sweet tonight. Via problems like (that our in-house designers have also suffered from), I put together a template tag called "extends_default" a couple months back.

The basic premise is to allow a template to fall back on a similarly named "default" template that occurs later in the TEMPLATE_DIRS. Hence, you could have a generic "blog/blog_detail.html" that you use for all of your sites and, when launching a new blog, have a lightly customized version in a different template directory. That lightly customized version would use the extends_default tag instead of extends and refer to the same file name.

When rendered, it will create nodes for the lightly-customized version, then go back through the TEMPLATE_DIRS until it finds another file of the same name that is NOT using extends_default. It will then pull in those nodes and render as normal.

Unfortunately, due to the naive (and very quick) nature of Django's templating system, this is limited to only one level deep and, as such, does not simulate true inheritance. I looked into doing this but could not come up with a thread-safe way that did not involve hacking up Django's internals.

Rather than providing a repository on this, I've posted the snippet to Mr. Bennett's djangosnippets site. You can find the source at I'd welcome any feedback on it.

Toast Driven