TypeError at /admin/booktest/book_infor/add/ __str__ returned non-string (type bytes)

Posted huao990928

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TypeError at /admin/booktest/book_infor/add/ __str__ returned non-string (type bytes)相关的知识,希望对你有一定的参考价值。

问题:

在Django的管理页面进行添加的时候

技术图片

 

 

出现了如下的错误

 TypeError at /admin/booktest/book_infor/add/ __str__ returned non-string (type bytes)

技术图片

 

 原因就在于你在写的models模块的时候没有注意你使用的是python2还是python3系列,两者实际上有着非常大的区别。就好像成人和小孩的区别一样

pyhotn2中的__str__():方法

1     def __str__(self):
2         return self.btitle.encode(utf-8)

python3中的__str__():方法

    def __str__(self):
        return self.btitle

因为在python3中会自动进行编码不需要指定编码格式,所以在python3中使用python2的那种格式就会报错

 

 

 

 

 

以上是关于TypeError at /admin/booktest/book_infor/add/ __str__ returned non-string (type bytes)的主要内容,如果未能解决你的问题,请参考以下文章

python继承初始化对象实例时 TypeError: module() takes at most 2 arguments (3 given)

IONIC : TypeError: Object(...) is not a function at Geolocation.getCurrentPosition

NodeJs 使用 ExpressJs : TypeError: string is not a function at Function.app.render

error = "TypeError: Object [object Object] has no method 'addUnique'\n at main.js:33:12";云

已解决 - 未捕获的 TypeError: $(...).datetimepicker is not a function at app.js:61

在vue-cli中使用axios时报错TypeError: Cannot set property 'lists' of undefined at eval