zoukankan      html  css  js  c++  java
  • 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')
    随缘
  • 相关阅读:
    HDU
    POJ
    POJ
    POJ
    POJ
    POJ
    POJ
    SPFA算法——最短路径
    POJ1251 Jungle Roads Kruskal+scanf输入小技巧
    MongoDB--关于数据库及选择MongoDB的原因
  • 原文地址:https://www.cnblogs.com/chang/p/2860342.html
Copyright © 2011-2022 走看看