Django等价于PHP的表单值数组/关联数组

时间:2022-03-30 19:20:09

In PHP, I would do this to get name as an array.

在PHP中,我这样做是为了获得一个数组的名称。

<input type"text" name="name[]" />
<input type"text" name="name[]" />

Or if I wanted to get name as an associative array:

或者如果我想把名字作为一个关联数组:

<input type"text" name="name[first]" />
<input type"text" name="name[last]" />

What is the Django equivalent for such things?

对于这样的事情,Django是什么意思?

3 个解决方案

#1


61  

Check out the QueryDict documentation, particularly the usage of QueryDict.getlist(key).

查看QueryDict文档,特别是使用QueryDict.getlist(key)。

Since request.POST and request.GET in the view are instances of QueryDict, you could do this:

因为请求。邮报》和请求。GET in the view are instances of QueryDict,你可以这样做:

<form action='/my/path/' method='POST'>
<input type='text' name='hi' value='heya1'>
<input type='text' name='hi' value='heya2'>
<input type='submit' value='Go'>
</form>

Then something like this:

然后是这样的:

def mypath(request):
    if request.method == 'POST':
        greetings = request.POST.getlist('hi') # will be ['heya1','heya2']

#2


18  

Sorry for digging this up, but Django has an utils.datastructures.DotExpandedDict. Here's a piece of it's docs:

不好意思,我把它翻出来了,Django有一个utils. datastructuretures.dotexpandeddict。这里有一个文档:

>>> d = DotExpandedDict({'person.1.firstname': ['Simon'], \
        'person.1.lastname': ['Willison'], \
        'person.2.firstname': ['Adrian'], \
        'person.2.lastname': ['Holovaty']})
>>> d
{'person': {'1': {'lastname': ['Willison'], 'firstname': ['Simon']}, '2': {'lastname': ['Holovaty'], 'firstname': ['Adrian']}}}

The only difference being you use dot's instead of brackets.

唯一的区别是你使用点而不是括号。

EDIT: This mechanism was replaced by form prefixes, but here's the old code you can drop in your app if you still want to use this concept: https://gist.github.com/grzes/73142ed99dc8ad6ac4fc9fb9f4e87d60

编辑:这个机制已经被表单前缀所取代,但是如果您还想使用这个概念,可以在应用程序中插入一些旧代码:https://gist. github.com/grzes/73142ed99dc8ad4fc9fc9fb9ffb4e87d60

#3


5  

Django does not provide a way to get associative arrays (dictionaries in Python) from the request object. As the first answer pointed out, you can use .getlist() as needed, or write a function that can take a QueryDict and reorganize it to your liking (pulling out key/value pairs if the key matches some key[*] pattern, for example).

Django不提供从请求对象获取关联数组(Python中的字典)的方法。正如第一个答案指出的那样,您可以根据需要使用.getlist(),或者编写一个函数,它可以使用QueryDict并将其重新组织到您喜欢的地方(例如,如果键匹配某个关键的[*]模式),就可以取出键/值对。

#1


61  

Check out the QueryDict documentation, particularly the usage of QueryDict.getlist(key).

查看QueryDict文档,特别是使用QueryDict.getlist(key)。

Since request.POST and request.GET in the view are instances of QueryDict, you could do this:

因为请求。邮报》和请求。GET in the view are instances of QueryDict,你可以这样做:

<form action='/my/path/' method='POST'>
<input type='text' name='hi' value='heya1'>
<input type='text' name='hi' value='heya2'>
<input type='submit' value='Go'>
</form>

Then something like this:

然后是这样的:

def mypath(request):
    if request.method == 'POST':
        greetings = request.POST.getlist('hi') # will be ['heya1','heya2']

#2


18  

Sorry for digging this up, but Django has an utils.datastructures.DotExpandedDict. Here's a piece of it's docs:

不好意思,我把它翻出来了,Django有一个utils. datastructuretures.dotexpandeddict。这里有一个文档:

>>> d = DotExpandedDict({'person.1.firstname': ['Simon'], \
        'person.1.lastname': ['Willison'], \
        'person.2.firstname': ['Adrian'], \
        'person.2.lastname': ['Holovaty']})
>>> d
{'person': {'1': {'lastname': ['Willison'], 'firstname': ['Simon']}, '2': {'lastname': ['Holovaty'], 'firstname': ['Adrian']}}}

The only difference being you use dot's instead of brackets.

唯一的区别是你使用点而不是括号。

EDIT: This mechanism was replaced by form prefixes, but here's the old code you can drop in your app if you still want to use this concept: https://gist.github.com/grzes/73142ed99dc8ad6ac4fc9fb9f4e87d60

编辑:这个机制已经被表单前缀所取代,但是如果您还想使用这个概念,可以在应用程序中插入一些旧代码:https://gist. github.com/grzes/73142ed99dc8ad4fc9fc9fb9ffb4e87d60

#3


5  

Django does not provide a way to get associative arrays (dictionaries in Python) from the request object. As the first answer pointed out, you can use .getlist() as needed, or write a function that can take a QueryDict and reorganize it to your liking (pulling out key/value pairs if the key matches some key[*] pattern, for example).

Django不提供从请求对象获取关联数组(Python中的字典)的方法。正如第一个答案指出的那样,您可以根据需要使用.getlist(),或者编写一个函数,它可以使用QueryDict并将其重新组织到您喜欢的地方(例如,如果键匹配某个关键的[*]模式),就可以取出键/值对。