Dan Ward Follow me on Twitter View my LinkedIn profile Subscribe to my news feed   

A self-confessed geek and web developer

 

Blog: Loading Django template tags and filters automatically (without the {% load %} tag)

From time-to-time, I'll have a brainwave while trying to figure out something tech-oriented; well, either that or I just want to get something off my chest. Anyhow, if I get round to typing it up, it'll wind up here in the blog.

Created by dan on Thursday 18th February, 2010 at 5:12 PM
Tags: Django, Python, Web development

I'm not entirely sure of how well this is documented on Django's part, however using the {% load %} tag on each page template where tags and filters used surely break the DRY principle? Anyway, there is an easier way to load tags and filters for all templates.

There's just a couple of lines required to include a template tag or filter library, and you can include more than one library if you so require (like I do). Simply place the code below in to your __init__.py file and off you go...

from django.template import add_to_builtins
add_to_builtins('path.to.templatetags.tags.py')

It is my understanding that you can place this in whichever __init__.py file you wish, however it'll only load the tags or filters to views, which are at the same level or below the __init__.py file you place the lines in.