开发者

Handling IRI in Django

The Django documentation has a [nice section] on handling strings with non-AS开发者_开发百科CII data in URLs. In particular, it presents the following example on how to transform Unicode strings for use in URLs:

>>> urlquote(u'Paris & Orléans')
u'Paris%20%26%20Orl%C3%A9ans'
>>> iri_to_uri(u'/favorites/François/%s' % urlquote(u'Paris & Orléans'))
'/favorites/Fran%C3%A7ois/Paris%20%26%20Orl%C3%A9ans'

However, there seems to be no indication on how to perform the reverse transformation!

Assuming that my application receives the URL /favorites/Fran%C3%A7ois/Paris%20%26%20Orl%C3%A9ans, how do I map that back to /favorites/François/ and Paris & Orléans?

There is no django.utils.encoding.uri_to_iri function to complement django.utils.encoding.iri_to_uri and there is no django.utils.http.urlunquote to complement django.utils.http.urlquote()!

Note:

If this helps at all, I'm using Django 1.2 over

  • Python 2.5, Debian Linux 32-bit
  • Python 2.6, Windows 7 64-bit.


The standard urllib.unquote() should work just fine in this case:

>>> urllib.unquote('/favorites/Fran%C3%A7ois/Paris%20%26%20Orl%C3%A9ans')
'/favorites/Fran\xc3\xa7ois/Paris & Orl\xc3\xa9ans'


That's because urllib.unquote does this for you:

>>> import urllib
>>> print urllib.unquote('/favorites/Fran%C3%A7ois/Paris%20%26%20Orl%C3%A9ans')
/favorites/François/Paris & Orléans
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜