我尝试测试一个视图,我收到来自IPad的json请求,格式为:
req = {"custom_decks": [ { "deck_name": "deck_test","updates_last_applied": "1406217357","created_date": 1406217380,"slide_section_ids": [ 1 ],"deck_id": 1 } ],"custom_decks_to_delete": [] }
我在jsonlint中检查过,它通过了.
我通过以下方式发布要求:
response = self.client.post('/library/api/6.0/user/'+ uuid + '/store_custom_dec/',content_type='application/json',data=req)
视图返回“creation_success”:false
问题是post方法在视图中没有找到关键的custom_decks.
QueryDict: {u'{"custom_decks": [{"deck_id": 1,"slide_section_ids": [1],"created_date":1406217380,"deck_name": "deck_test"}],"custom_decks_to_delete": []}': [u'']}>
问题是post方法在视图中没有找到关键的custom_decks.
因为它是用一个键把我的dict转换成QueryDict.
我很感激所有的帮助.
谢谢
解决方法
您正在发布JSON,与表单编码数据不同.您需要获取request.body的值并反序列化它:
data = json.loads(request.body) custom_decks = data['custom_decks']