django测试客户端不调用POST,而是GET [英] django test client doesnt call POST, but GET
问题描述
我尝试测试通过POST调用的视图.我使用 follow = True .但是测试客户端使用GET方法,并且我的POST数据没有传递.
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.
这是我的观点:
def aaa(request):
n = request.method
d = request.POST
template = 'shop/test.html'
return render(request, template, d)
这是我的考试:
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)
调用视图时.它不是POST,但是使用GET和我的POST参数当然是空的.有人可以说为什么会发生吗?
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?
我用新鲜的Django做了一个干净的venv.而且它按预期方式工作(称为POST),看起来在丹麦州有些烂掉了.
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.
推荐答案
follow=True
表示客户端遵循重定向.
means that the client follows the redirection.
response = self.client.post('/aaa/', {'product': 11}, follow=True)
表示该响应包含以下响应内容.您的测试没有错;它必须正在执行POST.
means that the response contains the followed response content. There is nothing wrong with your test; it must be doing a POST.
奇怪的是您的视图没有重定向到任何东西,所以我不明白您为什么使用follow = True.另外,我不明白您为什么认为该帖子不起作用.您的测试结果是什么?
What's weird is that your view doesn't redirect to anything so I don't understand why you use follow=True. Also I don't see why you assume that post isn't working. What's the result of your test?
这篇关于django测试客户端不调用POST,而是GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!