0

I try to test view calling by POST. I use follow=True. But test client uses GET method and my POST data are not passed.

here is my view:

def aaa(request):
    n = request.method
    d = request.POST
    template = 'shop/test.html'
    return render(request, template, d)

Here is my test:

from django.utils import unittest
from django.test.client import Client

def test_add_to_cart_page(self):
    response = self.client.post('/aaa/', {'product': 11}, follow=True)
    self.assertEqual(response.status_code, 200)

When the view is called. It is not POST, but GET used and my POST params are empty of course. Can somebody say why its happened?

EDIT: I made a clean venv with fresh Django. And it works as expected(calls POST) Looks like there is something rotten in the state of Denmark.

4

2 回答 2

2
follow=True

表示客户端遵循重定向。

response = self.client.post('/aaa/', {'product': 11}, follow=True)

表示响应包含后面的响应内容。你的测试没有问题;它必须在做一个 POST。


奇怪的是你的视图没有重定向到任何东西,所以我不明白你为什么使用 follow=True。另外,我不明白您为什么认为该帖子不起作用。你的测试结果如何?

于 2015-02-04T02:44:20.540 回答
0

经过调查,我意识到使用 PREPEND_WWW 会破坏测试客户端的发布请求。

于 2015-02-05T09:18:34.280 回答