Django custom template filters
By Maxime Biais, Thursday 11 January 2007 at 21:27 :: Django :: #11 :: rss
Django template filters are like unix pipes. In this example, we want to create a filter that take a $Date$ Subversion keyword and modify the date and time string format. Note: that also work with CVS, because CVS and Subversion shares the same keyword syntax.
Create a templatetags directory in you application folder, touch a __init_.py and a file where you are going to write your filters, for this example svn_keyword_filters.py
You should have a directory tree looking like:
myapp/
views.py
templatetags/
__init__.py
svn_keyword_filters.py
In svn_keyword_filters.py write:
from django import template register = template.Library() @register.filter("svndate") def svndate(value): return value[7:26]
In the template add something like:
<p> {% load svn_keyword_filters %} {{ "$Date: 2006-11-02 11:45:14 +0100 (Thu, 02 Nov 2006) $"|svndate }} </p>
Produces this html output
<p> 2006-11-02 11:45:14 </p>
Check the Django documentation about custom template filters.



Comments
1. Thursday 30 August 2007 at 22:40, by Mike
2. Friday 28 September 2007 at 20:34, by Michael
3. Friday 28 March 2008 at 14:00, by Philippe Mironov
Write your comment