Tay Ray Chuan home archive

custom template tags in GAE django

Mon, 23 May 2011 01:55:19 +0800 | Filed under google app engine, django

In vanilla django, if you wanted to `{% load foo_tags %}, what you did wsa to create a importable templatetags namespace with a foo_tags.py like this:

|- settings.py
|- manage.py
|- ...
|- templatetags/
   |- __init__.py
   |- foo_tags.py

But with Google App Engine's django (0.96), it's not so easy. The tag library mechanism tries to load django.templatetags.foo_tags, which, for obvious reasons, will fail.

Here's what I came up with:

if not template.libraries.get('django.templatetags.bloggart_tags', None):
  import bloggart_tags as tag_lib
  template.libraries['django.templatetags.bloggart_tags'] = tag_lib.register

As you can guess, I ran into this problem while trying to add timezone offsets to atom.xml timestamps in bloggart; you can see the feature branch here.

While examining the code base for django 1.2 packaged with the GAE Python SDK, I believe that this issue can be resolved by just switching to django 1.2, but this will have to wait, as I'm too lazy to touch settings.py and create a main.py with django config.

blog comments powered by Disqus