So I created a custom Django template filter to circumvent this issue:
Note: I have only tested it in Google App Engine (GAE) using its Django 0.96 template support. Also, the registration mechanism described below is specific to Google App Engine's own templating engine. However, there is no reason why this custom filter would not work in non-GAE Python apps using Django 0.96 or 1.0 templates. Please follow instructions here to configure your app if you are not using GAE.
The custom tag (which handles both unicode and "non-unicode" characters) itself is very straightforward to create.
I created a module customtags.xss (you can name it something else) as follows:
1) under the root folder of my GAE application, I created a folder "customtags"
2) inside customtags, I created two empty files xss.py and __init__.py (__init__.py will remain empty)
3) inside xss.py, I put
import typesSo, the filter utf-8 encodes the string first if it is of type UnicodeType. (You may want to handle exceptions more gracefully though - at the moment, I am not catching exceptions...)
import urllib
from django import template
register = template.Library()
@register.filter
def unicode_urlencode(value):
if type(value) is types.UnicodeType:
return urllib.quote(value.encode("utf-8"))
else:
return urllib.quote(value)
Let's look at how to register this template library so that you can use the filter inside your Django templates.
In your application script, for example, YOUR_APP_NAME.py, simply insert the line:
webapp.template.register_template_library("customtags.xss")after the import statements at the top of the script. Obviously, if you are calling your module something else, you have to change the line above accordingly.Now you can write <a href="/?{{ unsafe_string|unicode_urlencode }}" title="..">..</a> in your template!
1 comment:
Awesome. Thank you!
Post a Comment