Django请求发布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
问题是post方法在视图中找不到关键字custom_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'']}>
问题是Post方法在视图中找不到关键的custom_decks。
因为它是用一个键将我的dict转换成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请求发布json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!