Skip to main content
 首页 » 编程设计

python之Django APIClient 发布为空

2024年12月31日27jackei

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

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

这是 View :

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

请您参考如下方法:

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中获取数据