我正在使用 pylons,并且我的一些 url 包含非英文字符,例如:
http://localhost:5000/article/111/文章标题
在大多数情况下,这不是问题,但是在我的登录模块中,在用户注销后,我尝试referer
从中获取request.headers
,并重定向到该 url。
if user_logout:
referer = request.headers.get('referer', '/')
redirect(referer)
不幸的是,如果url中包含非英文字符,并且用IE浏览器会报这样的错误(Firefox是可以的):
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
有一种方法可以修复它(但不好),用于urllib.quote()
在重定向之前转换 url。
referer = quote_path(url) # only quote the path of the url
redirect(referer)
这不是一个好的解决方案,因为它只有在浏览器是 IE 的情况下才有效,而且非常无聊。有什么好的解决办法吗?