本人学习django项目搭建的时候遇到了这样一个问题,上网查询后成功解决。
1.问题再现
报错代码:AttributeError: 'str' object has no attribute 'decode'
报错截图:
2.上网搜索
参考网上大神给出的解释:
AttributeError: 'str' object has no attribute 'decode'
一般是因为str的类型本身不是bytes,所以不能解码
Python3的str 默认不是bytes,所以不能decode,只能先encode转为bytes,再decode
python2的str 默认是bytes,所以能decode
3.解决办法
那很显然我们只需要在出问题的代码之前转换一下就好了
找到报错代码

修改解决问题

最后成功运行
在学习Django项目搭建时遇到AttributeError: 'str' object has no attribute 'decode'的问题。经网上搜索了解到,此错误因字符串类型不是bytes导致。Python3的str默认不是bytes,需要先encode成bytes再decode。解决方案是找到报错代码位置,对str对象进行适当的encode和decode操作,从而成功解决问题。
722

被折叠的 条评论
为什么被折叠?



