django request get_full_path 中文问题

最近做日志记录,希望将request 请求的url 存储到log 文件中,但是啊

可恶的中文,在url中时经过编码的,直接存储的话,是%形式的

查询之后,说django 默认编码是unicode 的,想要显示原中文,必须经过处理

下面直接上代码

import urllib
full_path = request.get_full_path().encode('utf-8') full_path = urllib.url2pathname(full_path).decode('utf-8')

两行代码搞定。

另外记录一条,错误信息,肯能会同时遇到

UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe6 in position 26: ordinal not in range(128)

这样的话,最简单的办法就是

request.path.encode('utf-8')

 

 

posted @ 2013-01-14 21:35  深秋的黎明  阅读(1783)  评论(0编辑  收藏  举报