On Tue, 2007-06-05 at 14:34 +0300, Andrey Khavryuchenko wrote:
> Folks,
>
> I'm working on multiligual app using unicode branch and hit an interesting
> bit yesterday.
>
> I'm trying to present a (cyrillic) tag in url-ready form. Usually this is
> solved by
>
> {{ tagname|urlencode }}
>
> But when the 'tagname' contains non-ascii symbols, urlencode barfs:
>
> KeyError at /
> u'\u0420'
> Request Method: GET
> Request URL: http://localhost:8088/
> Exception Type: KeyError
> Exception Value: u'\u0420'
> Exception Location: /usr/lib/python2.4/urllib.py in quote, line 1117
> Template error
>
> My current (temporary) solution is to encode this manually, in python:
>
> urllib.quote(tagname.encode('utf8')
>
> But this is not DRY and makes me think there's a better way.
>
> Or it's time to add a custom filter?
Have a look at the iriencode filter (only in the unicode branch, so
you'll need to read docs/templates.txt from the source). This handles
the last stage of encoding to ASCII.
You cannot necessarily skip the urlencode portion, but you may be able
to. The reason is that the IRI -> URI conversion algorithm does _not_
encode things like '%', so it is safe to apply to something that has
already been partially encoded. However, if your proto-URL string
contains a '%' that should be encoded (e.g. "100%-guaranteed"), you will
need to pass it through urlencode first.
So {{ tagname|urlencode|iriencode }} is completely safe, although
possibly redundant (and even incorrect if tagname was something that had
already been URL encoded).
You might also want to read the section call "URI and IRI handling" in
docs/unicode.txt because urllib.quote() is not the most bullet-proof
choice when working with unicode strings. Reading your email, I just
realised I have forgotten to mention the urlencode and iriencode filters
in that section, but that will be fixed when I next sit down to commit
bug fixes.
Regards,
Malcolm
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---