librelist archives

« back to archive

Forms - submit values as a list?

Forms - submit values as a list?

From:
Malphas Wats
Date:
2012-05-21 @ 10:14
Hi,

  Another newbie question: Is it possible to submit a set of fields as a list?

In PHP, I would be able to do this:

  <form method="POST" action='/post'>
    <p><input type="text" name="first"></p>
    <p><input type="text" name="values[]"></p>
    <p><input type="text" name="values[]"></p>
    <p><input type="text" name="values[]"></p>
    <p><input type="submit" value="submit"></p>
  </form>

and there would be an array called 'values' in the $_POST.

when I tried the same in Flask, the values are all passed through to
the request.form object, but they all have the same key and are just
part of the list of other fields, making it difficult to extract them
on their own (or at all if I try to use the key, I only get the first
one!):

  print request.form

  >>> ImmutableMultiDict([('values[]', u'one'), ('values[]', u'two'),
('values[]', u'three'), ('first', u'first')])


  print request.form['values[]']

  >>> one

Re: [flask] Forms - submit values as a list?

From:
Ib Lundgren
Date:
2012-05-21 @ 10:39
Hey

I have not verified that this works with ImmutableMultiDict but it works
for list of 2-tuples.

def value_list(items, field):
   is_field = lambda x : x[1] if x[0] == field else None
   is_true = lambda x: True if x else False
   return filter(is_true, map(is_field, items))

>>> items = [('values[]', u'one'), ('values[]', u'two'), ('values[]',
u'three'), ('first', u'first')]
>>> value_list(items, "values[]")
[u'one', u'two', u'three']

-- Ib

On Mon, May 21, 2012 at 12:14 PM, Malphas Wats
<malphas@subdimension.co.uk>wrote:

> Hi,
>
>  Another newbie question: Is it possible to submit a set of fields as a
> list?
>
> In PHP, I would be able to do this:
>
>  <form method="POST" action='/post'>
>    <p><input type="text" name="first"></p>
>    <p><input type="text" name="values[]"></p>
>    <p><input type="text" name="values[]"></p>
>    <p><input type="text" name="values[]"></p>
>    <p><input type="submit" value="submit"></p>
>  </form>
>
> and there would be an array called 'values' in the $_POST.
>
> when I tried the same in Flask, the values are all passed through to
> the request.form object, but they all have the same key and are just
> part of the list of other fields, making it difficult to extract them
> on their own (or at all if I try to use the key, I only get the first
> one!):
>
>  print request.form
>
>  >>> ImmutableMultiDict([('values[]', u'one'), ('values[]', u'two'),
> ('values[]', u'three'), ('first', u'first')])
>
>
>  print request.form['values[]']
>
>  >>> one
>

Re: [flask] Forms - submit values as a list?

From:
Simon Sapin
Date:
2012-05-21 @ 10:46
Le 21/05/2012 12:14, Malphas Wats a écrit :
> Hi,
>
>    Another newbie question: Is it possible to submit a set of fields as a list?
>
> In PHP, I would be able to do this:
>
>    <form method="POST" action='/post'>
>      <p><input type="text" name="first"></p>
>      <p><input type="text" name="values[]"></p>
>      <p><input type="text" name="values[]"></p>
>      <p><input type="text" name="values[]"></p>
>      <p><input type="submit" value="submit"></p>
>    </form>
>
> and there would be an array called 'values' in the $_POST.
>
> when I tried the same in Flask, the values are all passed through to
> the request.form object, but they all have the same key and are just
> part of the list of other fields, making it difficult to extract them
> on their own (or at all if I try to use the key, I only get the first
> one!):
>
>    print request.form
>
>    >>>  ImmutableMultiDict([('values[]', u'one'), ('values[]', u'two'),
> ('values[]', u'three'), ('first', u'first')])
>
>
>    print request.form['values[]']
>
>    >>>  one


The [] in the field name is not meaningful for Flask, you can just use 
name="values".

request.form is a MultiDict. request.form['foo'] only gives you the 
first value, but you can get the list of all values with 
request.form.getlist('foo')

The complete MultiDict API is documented here:

http://werkzeug.pocoo.org/docs/datastructures/#werkzeug.datastructures.MultiDict

Regards,
-- 
Simon Sapin

Re: [flask] Forms - submit values as a list?

From:
Serge S. Koval
Date:
2012-05-21 @ 10:42
Hi,

 Use request.form.getlist('values[]').

 See MultiDict doc here:

http://werkzeug.pocoo.org/docs/datastructures/#werkzeug.datastructures.MultiDict.getlist

Serge.

On Mon, May 21, 2012 at 1:14 PM, Malphas Wats <malphas@subdimension.co.uk>wrote:

> Hi,
>
>  Another newbie question: Is it possible to submit a set of fields as a
> list?
>
> In PHP, I would be able to do this:
>
>  <form method="POST" action='/post'>
>    <p><input type="text" name="first"></p>
>    <p><input type="text" name="values[]"></p>
>    <p><input type="text" name="values[]"></p>
>    <p><input type="text" name="values[]"></p>
>    <p><input type="submit" value="submit"></p>
>  </form>
>
> and there would be an array called 'values' in the $_POST.
>
> when I tried the same in Flask, the values are all passed through to
> the request.form object, but they all have the same key and are just
> part of the list of other fields, making it difficult to extract them
> on their own (or at all if I try to use the key, I only get the first
> one!):
>
>  print request.form
>
>  >>> ImmutableMultiDict([('values[]', u'one'), ('values[]', u'two'),
> ('values[]', u'three'), ('first', u'first')])
>
>
>  print request.form['values[]']
>
>  >>> one
>