pythonurlpylonsnon-englishwebob

How to redirect to a url with non-English characters?


I'm using pylons, and some of my urls contains non-English characters, such as:

http://localhost:5000/article/111/文章标题

At most cases, it won't be a problem, but in my login module, after a user has logging out, I try to get the referer from the request.headers, and redirect to that url.

if user_logout:
    referer = request.headers.get('referer', '/')
    redirect(referer)

Unforunately, if the url contains non-English characters, and with a brower of IE, it will report such an error (Firefox is OK):

  WebError Traceback:
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xd5 in position 140: ordinal not in range(128) 
View as:   Interactive (full)  |  Text (full)  |  XML (full) clear this 
clear this 
URL: http://localhost:5000/users/logout

Module weberror.evalexception:431 in respond          view

There is a way to fix it(but no good), use urllib.quote() to convert the url before redirecting.

referer = quote_path(url) # only quote the path of the url
redirect(referer)

This is not a good solution, because it only works if the brower is IE, and very boring. Is there any good solution?


Solution

  • At last, I still not find a good solution, and use this code:

    referer = urllib.quote(referer, '.:/?=;-%#')
    

    It seems work well now, but I don't feel safe.