简体   繁体   English

使用Django / Python阅读多维发布请求

[英]Read a multidimensional post request using Django / Python

I'm sending a post request like this: 我正在发送这样的帖子请求:

photo[1][id] = 1234
photo[1][size] = 4x4
photo[1][quantity] = 2
photo[2][id] = 4567
photo[2][size] = 4x6
photo[2][quantity] = 1
...

What is the best way to read this data using Django/Python? 使用Django / Python读取此数据的最佳方法是什么?

Thanks!! 谢谢!!

You might want to try querystring-parser . 您可能要尝试使用querystring-parser

For example, if you had the following form being submitted via POST to your view: 例如,如果您有以下表单是通过POST提交到视图的:

<input name="photo['1']['id']"       value="1234">
<input name="photo['1']['size']"     value="4x4">
<input name="photo['1']['quantity']" value="2">
<input name="photo['2']['id']"       value="4567">
<input name="photo['2']['size']"     value="4x6">
<input name="photo['2']['quantity']" value="1">

In your view you can parse it like this: 在您看来,您可以这样解析它:

from querystring_parser import parser
post_dict = parser.parse(request.POST.urlencode())
print post_dict
# {u'csrfmiddlewaretoken': u'<crazy hash goes here>', 
#  u'photo': 
#    {1: {u'id': u'1234', u'size': u'4x4', u'quantity': u'2'},
#     2: {u'id': u'4567', u'size': u'4x6', u'quantity': u'1'}
#  }

Accessing the first photo's size is as simple as post_dic[1]['size'] 访问第一张照片的大小就像post_dic[1]['size']一样简单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM