python---补充django中文报错(2),Django3.5出错

今天是要Django3.5设置项目,结果出现中文报错,虽然之前分析过py2.7的报错原因,但是在py3之后reload不在使用,需要引入:

from importlib import  reload

但是这个并没有任何用,因为在py3之后默认编码不再是字节码,而是utf-8,可以使用代码查看

sys.getdefaultencoding()

这时候就出现了新的错误,出错地点

data = data.encode() AttributeError: ‘bytes‘ object has no attribute ‘encode

原本抱着不去修改源码的态度,找找其他解决办法,但是找了半天,结果没找到.....。而且这也不是因为中文问题了,这尼玛刚刚说了,已默认utf-8编码了,

所以算了,直接修改源码看看如何

    def finish_response(self):
        """Send any iterable data, then close self and the iterable

        Subclasses intended for use in asynchronous servers will
        want to redefine this method, such that it sets up callbacks
        in the event loop to iterate over the data, and to call
        ‘self.close()‘ once the response is finished.
        """
        try:
            if not self.result_is_file() or not self.sendfile():
                for data in self.result:
                    # data = data.encode()
                    self.write(data)
                self.finish_content()
        finally:
            self.close()

结果居然成功了。

再调试一下,看看原因

                for data in self.result:
                    print(data,type(data))  #发现这个数据原本类型就是字节型,不需要我们再次进行编码。而且字节型也没有这个属性,这就是报错的原因
                    # data = data.encode()
                    self.write(data)
                self.finish_content()        

原文地址:https://www.cnblogs.com/ssyfj/p/8732665.html

时间: 2024-08-07 22:59:28

python---补充django中文报错(2),Django3.5出错的相关文章

python---补充django中文报错

SyntaxError at /blog/ news/story Non-ASCII character '\xe4' in file D:\MyPython\day23\HelloWorld\blog\views.py on line 42, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details (views.py, line 42) 当使用中文时会报错: def introduce(req

sublime text 2编译Python时打印中文报错的解决方案

当用sublime text 2 编译 python 文件时,若 print 打印出的中文时,控制台会报错: [Decode error - output not utf-8] 解决方案如下: 打开 sublime text 2 首选项 -> 浏览插件,进入 Python 文件夹,并找到 Python.sublime-build 文件. 打开如下: { "cmd": ["python", "-u", "$file"],

django 中文报错

在Django视图函数中经常出现类似于'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)的错误. 在解决错误之前,首先要了解unicode和utf-8的区别. unicode指的是万国码,是一种"字码表".而utf-8是这种字码表储存的编码方法.unicode不一定要由utf-8这种方式编成bytecode储存,也可以使用utf-16,utf-7等其他方式.目前大多都以utf-8的方

python在eclipse 中文报错解决 SyntaxError: Non-ASCII character

原因是在Win7系统的简体中文环境下,默认使用的中文编码格式是GBK格式,而eclipse默认的编码格式为使用所在系统的编码格式.因此需要对eclipse进行默认编码格式的修改. 步骤 打开eclipse,Windows -> Preferences... 左侧导航到general -> Workspace,在右侧的Text file encoding中点击Other,选择UTF-8,点击OK. 步骤2 Windows -> Preferences... 左侧导航到general -&g

新装python环境启动django程序报错

个人独立博客 http://www.xbman.cn/ 出处:http://www.xbman.cn/article/1 django启动报错 django.core.exceptions.ImproperlyConfigured: Error loading either pysqlite2 or sqlite3 modules (tried in that order): No module named _sqlite3 解决方法 centos yum install python-deve

django admin 中文报错incorrect string value解决方案

对于错误" Incorrect string value: '\xE6\xA2\xB5\xE8\x92\x82...'for column 'object_repr' at row 1 解决方法是设置django_admin_log表的object_repr一项使用utf8_unicode_ci: 对于错误"  Incorrect string value: '\xE6\xA2\xB5\xE8\x92\x82...'for column 'change_message' at row

Django 运行报错 ImportError: No module named 'PIL'

importError: No module named pil WIN7 64位系统安装 Python PIL 首先通过easy_install安装 说找不到pil模块. 第二通过去官网找:http://www.pythonware.com/products/pil/ 找了几个版本安装版的没有64位的,源码包的下载无法安装说要安装vs2008我也安装了还是不行.最后在这个地址找到:http://www.lfd.uci.edu/~gohlke/pythonlibs/ Pillow is a re

Pycharm中不支持中文编码的解决方案。Pycharm中文报错。 Pycharm出现的部分快捷键无效及解决办法

Pycharm中不支持中文编码的解决方案.Pycharm中文报错. 1. 打开Pycharm ---->  File ----> Default setting ------> Editor -------> File Encodings ,如下图,设置成UTF-8, 然后应用 2.  点击[File]---[Setting]---[Editor]---[File and Code Templates],点击右边的[python script],在编辑框中输入: #-*-codin

Python读取txt文件报错:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 0

Python使用open读取txt中文内容的文件时,有可能会报错,报错内容如下:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 0. 这里举一个例子:txt=open("threekingdoms.txt","r",encoding="utf-8").read(),在运行时就会报错. 要处理这个问题有两个办法,如下: 第一个办法,将编码方式由utf-8改为g