Jun
19
2008

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

Written by w00kie in: Work stuff, internet | Tags: , ,

7 Comments »

  • Comment | August 15, 2008
    Sav

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

  • Comment | September 11, 2008
    Steve

    Thanks very much for this.

  • Comment | December 5, 2008
    Maluku

    Awesome, thank you very much

  • Comment | January 4, 2009
    Big Barry Banossi

    I’ve just discovered custom filters thanks to this posts – and DAMN aren’t they powerful! Solved a bunch of stuff for me and made everything so small, neat and simple. Thank you HEAPS for this info.

  • March 2, 2009
    Pingback from: Google AppEngine and Facebook Applications - 10 Things I wish I had known :: wubbahed.com
  • Comment | May 1, 2009
    dH

    Thanks a lot, works like charm!

  • Comment | June 5, 2009

    This was great. Thanks, just what I needed. Very concise. Cheers!

RSS feed for comments on this post. TrackBack URL

Leave a comment

For a cool icon next to your comment, use gravatar.

Powered by WordPress. Theme adapted from Aeros.