Django 请求 Post json [英] Django request Post json
问题描述
我尝试测试一个视图,收到来自iPad的json请求,格式为:
I try to test a view, I receive a json request from the IPad, the format is:
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 中检查过,它通过了.
I checked this in jsonlint and it passed.
我通过以下方式发布请求:
I post the req via:
response = self.client.post('/library/api/6.0/user/'+ uuid +
'/store_custom_dec/',content_type='application/json', data=req)
视图返回creation_success":false
The view return "creation_success": false
问题是view中的post方法没有找到keycustom_decks.
The problem is the post method in view doesn't find the key 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'']}>
问题是view中的post方法没有找到keycustom_decks.因为它用一键将我的字典转换为 QueryDict.
The problem is the post method in view doesn't find the key custom_decks. Because it is converting my dict to QueryDict with one key.
感谢所有帮助.
谢谢
推荐答案
您发布的是 JSON,它与表单编码数据不同.您需要获取 request.body
的值并将其反序列化:
You're posting JSON, which is not the same as form-encoded data. You need to get the value of request.body
and deserialize it:
data = json.loads(request.body)
custom_decks = data['custom_decks']
这篇关于Django 请求 Post json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!