Django IRI Handling

The Django documentation has a [nice section] for handling strings with non-ASCII data in URLs. In particular, he presents the following example of how to convert 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 inverse transform!

Assuming my application gets the URL /favorites/Fran%C3%A7ois/Paris%20%26%20Orl%C3%A9ans , how do I match it with /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 that helps at all, I'm using Django 1.2 on top

  • Python 2.5 32-bit Debian Linux
  • Python 2.6, 64-bit version of Windows 7.
+4
source share
2 answers

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

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

This is 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 
+1
source

All Articles