简体   繁体   English

从Django中的POST请求中读取多维数组

[英]Reading multidimensional arrays from a POST request in Django

I have a jquery client that is sending a POST request with a multidimensional array, something like this: 我有一个jquery客户端发送带有多维数组的POST请求,如下所示:

 friends[0][id] 12345678 friends[0][name] Mr A friends[1][id] 78901234 friends[1][name] Mr B 

That is, an array of two items, name and id. 也就是说,一个包含两个项目的数组,name和id。

Is there an automatic way to receive this input as list or a dictionary? 是否有自动方式接收此输入作为列表或字典? I can't seem to be able to make .getlist work 我似乎无法使.getlist工作

DrMeers' link is no longer valid, so I'll post another method of achieving the same thing. DrMeers的链接不再有效,所以我将发布另一种实现相同功能的方法。 It's also not perfect, and it would be much better if Django had such a function built-in. 它也不完美,如果Django内置了这样的功能会好得多。 But, since it doesn't: 但是,因为它没有:

Converting Multi-dimensional Form Arrays in Django 在Django中转换多维表格数组

Disclaimer: I wrote that post. 免责声明:我写了这篇文章。 The essence of it is in this function, which could be more robust, but it works for arrays of one-level objects: 它的本质是在这个函数中,它可以更健壮,但它适用于一级对象的数组:

def getDictArray(post, name):
    dic = {}
    for k in post.keys():
        if k.startswith(name):
            rest = k[len(name):]

            # split the string into different components
            parts = [p[:-1] for p in rest.split('[')][1:]
            print parts
            id = int(parts[0])

            # add a new dictionary if it doesn't exist yet
            if id not in dic:
                dic[id] = {}

            # add the information to the dictionary
            dic[id][parts[1]] = post.get(k)
    return dic

This is related with that question . 这与该问题有关 As stated there, I made special library for Django/Python to handle multidimensional arrays sent through requests. 如上所述,我为Django / Python创建了一个特殊的库来处理通过请求发送的多维数组。 You can find it on GitHub here . 你可以找到它在GitHub上这里

Does this help? 这有帮助吗? http://dfcode.com/blog/2011/1/multi-dimensional-form-arrays-and-django/ http://dfcode.com/blog/2011/1/multi-dimensional-form-arrays-and-django/

If you want POST data, the only way to get it is to specify the exact 'name' you are looking for: 如果您想要POST数据,获得它的唯一方法是指定您要查找的确切“名称”:

  person[1].name = request.POST['person[1][name]'] person[1].age = request.POST['person[1][age]'] person[2].name = request.POST['person[2][name]'] person[2].age = request.POST['person[2][age]'] 

Here is a quick on-the-fly workaround in Python when you have the need to extract form values without explicitly typing the full name as a string: 当您需要提取表单值而不显式键入全名作为字符串时,这是Python中的快速动态解决方法:

  person_get = lambda *keys: request.POST[ 'person' + ''.join(['[%s]' % key for key in keys])] 

Now when you need information, throw one of these suckers in and you'll have much wider flexibility. 现在,当您需要信息时,抛出其中一个吸盘,您将拥有更大的灵活性。 Quick example: 快速举例:

  person[1].name = person_get('1', 'name') person[1].age = person_get('1', 'age') person[2].name = person_get('2', 'name') person[2].age = person_get('2', 'age') 

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

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