Custom Django filters in Google App Engine

You want to create your own custom Django filters in App Engine without running a whole Django stack? Here's how in a few lines of code.

First create a specific python file to hold your custom filters at the root of your application. In my case I use customfilters.py like this:

import re
from google.appengine.ext import webapp
 
register = webapp.template.create_template_register()
 
def escapeimg(body):
	return re.sub(r'<img (.*)/>', '[IMG]', body)
 
register.filter(escapeimg)

Then in your main application source file, call the following line outside of the main() definition, for example just after your modules loading:

"""Load custom Django template filters"""
webapp.template.register_template_library('customfilters')

You should then be able to use the new filters you registered in customfilters.py straight away in any of your Django templates without any % load foobar % call

Tags: , ,

2 Comments »

  1. Sav said,

    August 15, 2008 @ 1:19 am

    Wow!!! Thanks for this post, it was a huge, huge help.

  2. Steve said,

    September 11, 2008 @ 10:37 am

    Thanks very much for this.

RSS feed for comments on this post · TrackBack URI

Leave a Comment