Sil*_*tor 3 python testing django view apiclient
我正在为帖子视图编写测试。它确实有效,但是当我尝试使用 APIClient.post 发布到它时,我得到 QueryDict: {}。这是测试:
class SMSCreateData(APITestCase):
...
def test_SMS(self):
...
postData = {'Body': string, 'From': phNum.phone_number}
self.client.post(reverse('SMS-data'), postData)
Run Code Online (Sandbox Code Playgroud)
这是视图:
def SMSSubmitDataPointView(request):
...
try:
print request.POST
...
Run Code Online (Sandbox Code Playgroud)
对您的帖子数据进行 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'
)
Run Code Online (Sandbox Code Playgroud)
您将获得数据request.POST
| 归档时间: |
|
| 查看次数: |
1311 次 |
| 最近记录: |