Django报错 query = query.decode(errors=‘replace‘)AttributeError: ‘str‘ object has no attribute ‘decod

具体错误

由于您正在使用 Python 3.x 版本并尝试使用 Python 2.x 版本的方法。在 Python 2.x 中,str 类型是字节字符串,因此可以使用 decode 方法将其转换为 Unicode 字符串。但是,在 Python 3.x 中,str 类型默认为 Unicode 字符串,因此不再具有 decode 方法。

这个错误通常会在使用 Python 3.x 和 Django 1.x 以及使用 MySQL 数据库时发生。原因是,在 Python 3.x 中运行python2.x编写的代码,里面的字符串是 Unicode 类型,不再具有 decode 方法。

网上的解决办法是打开相应的文件把146行的decode修改为encode

但是我无法修改这个文件,所以可以尝试下面这个方法

修改代码为这个

 

猜你喜欢

转载自blog.csdn.net/qq_52351946/article/details/131263867