使用Django/Python阅读多维发布请求 [英] Read a multidimensional post request using Django / Python

查看:62
本文介绍了使用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
...

使用Django/Python读取此数据的最佳方法是什么?

What is the best way to read this data using Django/Python?

谢谢!

推荐答案

您可能想尝试 querystring-parser .

例如,如果您有以下表单是通过POST提交到视图的:

For example, if you had the following form being submitted via POST to your view:

<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'}
#  }

访问第一张照片的大小就像post_dic[1]['size']

Accessing the first photo's size is as simple as post_dic[1]['size']

这篇关于使用Django/Python阅读多维发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆