gpt4 book ai didi

python - 属性错误: 'WSGIRequest' object has no attribute 'status_code'

转载 作者:行者123 更新时间:2023-11-30 22:17:51 24 4
gpt4 key购买 nike

我收到错误,AttributeError: 'WSGIRequest' 对象没有属性 'status_code'。我编写了测试代码,

def test_user(self):
obj = {
"username": "tom",
"email": "tom@gmail.com",
"password": ""
}

factory = APIRequestFactory()
response = factory.post('http://127.0.0.1:8000/polls/data/', obj)
self.assertEquals(response.status_code, 400)

但是当我运行这段代码时,发生了错误。我关注了这个网站 http://www.django-rest-framework.org/api-guide/testing/编写测试代码,我想为什么会发生这个错误。当然,响应类型与我理想的不同,但是我应该做什么?我该如何解决这个问题?

最佳答案

factory.post('http://127.0.0.1:8000/polls/data/', obj) 返回 request 实例。您需要将其传递给 View 来测试您的 View 方法:

request = factory.post('http://127.0.0.1:8000/polls/data/', obj)
response = your_view(request)

参见示例here .

关于python - 属性错误: 'WSGIRequest' object has no attribute 'status_code' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49527421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com