Django APIClient Post 空

Django APIClient Post Empty

我正在为 post 视图编写测试。它确实有效,但是当我尝试 post 和 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
...

urlencode 您的 post 数据并将 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

中获得数据