3

我正在为帖子视图编写测试。它确实有效,但是当我尝试使用 APIClient.post 发布到它时,我得到 QueryDict: {}。这是测试:

class SMSCreateData(APITestCase):
...
    def test_SMS(self):
        ...
        postData = {'Body': string, 'From': phNum.phone_number}
        self.client.post(reverse('SMS-data'), postData)

这是视图:

def SMSSubmitDataPointView(request):
...
    try:
        print request.POST
...
4

1 回答 1

4

对您的帖子数据进行urlencode并将content_type设置为application/x-www-form-urlencoded.

from urllib.parse import urlencode  
# In python 2, use this instead: from urllib import urlencode  


response = self.client.post(reverse('SMS-data'), urlencode(postData),
    content_type='application/x-www-form-urlencoded'
)

你会得到数据request.POST

于 2018-05-08T19:23:08.633 回答