Python,Django,报错信息:TypeError: a bytes-like object is required, not 'str'

我的代码,一个测试类:

class LoginActionTest(TestCase):
# 测试登陆动作
def setUp(self):
User.objects.create_user('admin1','[email protected]','admin123456')
# 数据初始化,创建一个用户
def test_login_action_username_password_null(self):
# 账密为空
testdata1={'username':'','password':''}
response1=self.client.post('/login_action/',data=testdata1)
self.assertEqual(response1.status_code,200)
self.assertIn('用户名或密码错误',response1.content)

运行后就报错如下:

然后我就上网搜这个报错,大致就是说有转义的问题,参考文档:

https://stackoverflow.com/questions/45250235/flask-unit-testing-and-not-understanding-my-fix-for-typeerror-a-bytes-like-obj

https://blog.csdn.net/qq_41185868/article/details/83833262

https://www.cnblogs.com/zhaijiahui/p/6926159.html

然后我就尝试打印出我的response1.content,发现打印内容中有一个b,如下图。

解决思路:根据那些参考文档说的,那我现在有两个办法,一个是把那个b去掉,一个是再转一次。

如何去掉b我没查,这里我用了第二个办法,代码如下:

扫描二维码关注公众号,回复: 6651382 查看本文章
self.assertIn('用户名或密码错误',response1.content.decode('utf-8'))

解决啦 

猜你喜欢

转载自www.cnblogs.com/ss0202go/p/11099306.html