如何重定向到非英文字符的网址?
我正在使用挂架,我的一些网址包含非英文字符,例如:
http://localhost:5000/article/111/文章标题
在大多数情况下,这不会有问题,但在我的登录模块中,在用户退出后,我尝试从request.headers
获取referer
,并重定向到该URL。
if user_logout:
referer = request.headers.get('referer', '/')
redirect(referer)
不幸的是,如果网址包含非英文字符,并且浏览器名称较大,则会报告此类错误(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()
转换网址。
referer = quote_path(url) # only quote the path of the url
redirect(referer)
这不是一个好的解决方案,因为它只适用于浏览器是IE,而且非常无聊。有什么好的解决方案吗?
没有找到相关结果
已邀请:
3 个回复
臀博
丧泉缝锋
雇砰
现在似乎运作良好,但我感觉不安全。