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')
    随缘
  • 相关阅读:
    MRO C3算法 super的运用
    约束 抛异常
    反射
    Ubuntu
    Vim
    Vim
    Arithmetic
    Docker-常用命令
    Docker
    Docker-LAMP开发环境
  • 原文地址:https://www.cnblogs.com/chang/p/2860342.html
Copyright © 2011-2022 走看看