【问题标题】:Read a multidimensional post request using Django / Python使用 Django / Python 读取多维发布请求
【发布时间】:2012-11-21 18:54:40
【问题描述】:

我正在发送这样的帖子请求:

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 读取这些数据的最佳方法是什么?

谢谢!!

【问题讨论】:

    标签: python django post multidimensional-array


    【解决方案1】:

    你可能想试试querystring-parser

    例如,如果您通过 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">
    

    在您看来,您可以像这样解析它:

    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']一样简单

    【讨论】:

      猜你喜欢
      • 2014-06-01
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      • 1970-01-01
      • 2011-03-25
      • 2018-04-04
      • 1970-01-01
      • 2015-10-09
      相关资源
      最近更新 更多