有没有一种方法可以在Django测试中获取发布请求的response.content?

时间:2020-01-19 13:45:47

标签: django

本文-https://www.dev2qa.com/how-to-use-django-test-client-to-test-views/-建议我们在发送发布请求后可以访问response.content以获取响应的内容。但是,我刚刚创建了一个死掉的简单项目和应用程序来对此进行测试,但是我只是得到了一个空字节串。我想念什么?

模型-

class Note(models.Model):
    text = models.CharField(max_length=30)

观看次数-

class CreateNote(CreateView):
    model = Note
    fields = '__all__'
    success_url = reverse_lazy("index")

def index(request):
    return HttpResponse("Hello!")

测试-

class NoteTest(TestCase):

    def test_createNote(self):
        response = self.client.post('/notes/create', {'text': 'hello world!'})
        print(response.content) # just returns b''

该文章是错误的。或该文章不涉及重定向。所以答案就是这样测试。

    def test_createNote(self):
        response = self.client.post('/notes/create', {'text': 'hello world!'}, follow=True)
        print(response.content)

0 个答案:

没有答案