开发者

request.POST pylons, getting array like in php

开发者 https://www.devze.com 2023-01-09 23:40 出处:网络
I have a dynamic form, i must bulid some post array to field witch some id. For example: <input type=\"checkbox\" name=\"field[124][]\" value=\"1\">

I have a dynamic form, i must bulid some post array to field witch some id.

For example:

<input type="checkbox" name="field[124][]" value="1">
<input type="checkbox" name="field[124][]" value="2">

In php i can simply get value and key.

foreach($_POST as $key => $value){
i开发者_开发技巧f(is_array($value){
  foreach($value as $key2 => $value2){
  //i get key=>124 and all values for this key
   }
}
}

<input type="checkbox" name="field" value="1">
<input type="checkbox" name="field" value="2">

In pylons for array of checkbox i can use

request.POST[field].getall()

How can i create in pylons post array like in PHP?

Thanks.


You can use .getall() of multidict object, for example:

html:

<input type="checkbox" name="field[124][]" value="1">
<input type="checkbox" name="field[124][]" value="2">

controller:

values = request.POST.getall('field[124][]')
# >>> values
# [u'1', u'2']

another way to get this list is by using .dict_of_lists(), example:

controller:

d = request.POST.dict_of_lists()
values = d['field[124][]']
# >>> d
# {'field[124][]':[u'1', u'2']}
# >>> values
# [u'1', u'2']
0

精彩评论

暂无评论...
验证码 换一张
取 消