Argument with multiple occurrence

129 views
Skip to first unread message

Sylvain

unread,
Oct 8, 2009, 9:59:50 AM10/8/09
to Tornado Web Server
Hello,

Like Google webapp : http://code.google.com/intl/fr/appengine/docs/python/tools/webapp/requestdata.html
it could be nice to have an "allow_multiple=True" in the get_argument
function. (or a get_all_argument)

Example : currently, if self.request.arguments returns {'key1':
['val3'], 'key2': ['val1', 'val2']}
self.get_argument(key2) will return 'val2'

and with an allow_multiple=True :
self.get_argument(key2, allow_multiple=True) it should return ['val1',
'val2']

I know it's possible to do : self.request.arguments['key2'], but here
we missed all unicode/utf-8 functions.

Another thing, in my example : {'key1': ['val3'], 'key2': ['val1',
'val2']}
For key1, that is not "multiple", self.request.arguments should return
{'key1': 'val3', 'key2': ['val1', 'val2']}.
i.e. : key1 should not be a list but a value.

I think it is the same issue with self.request.files, that always
returns a list even if there is only one file.

Regards

Sylvain

Eric Larson

unread,
Oct 8, 2009, 10:52:40 AM10/8/09
to python-...@googlegroups.com
At Thu, 8 Oct 2009 06:59:50 -0700 (PDT),

Just wanted to +1 this. Paste uses the idea of a MultiDict for post
vars with more than one value. This is commonly used when you have a
single form name that might have many selections such as a set of
checkboxes:

<input type="checkbox" name="traits[]" value="blue eyes" />
<input type="checkbox" name="traits[]" value="brown hair" />
<input type="checkbox" name="traits[]" value="male" />
...

I would expect the posted var to be a list in this case.

Thanks!

Eric Larson

Douglas Stanley

unread,
Oct 8, 2009, 11:23:42 AM10/8/09
to python-...@googlegroups.com
+1 for me too.

Riskable

unread,
Oct 9, 2009, 11:58:14 AM10/9/09
to Tornado Web Server
On Oct 8, 11:23 am, Douglas Stanley <douglas.m.stan...@gmail.com>
wrote:
> +1 for me too.
>
> On Thu, Oct 8, 2009 at 10:52 AM, Eric Larson <e...@ionrock.org> wrote:
>
> > At Thu, 8 Oct 2009 06:59:50 -0700 (PDT),
> > Sylvain wrote:
>
> >> Hello,
>
> >> Like Google webapp :http://code.google.com/intl/fr/appengine/docs/python/tools/webapp/req...
I made a patch for tornado.web that enables the behavior Sylvain has
requested (I think it is a good idea). You can download it here:

http://bit.ly/4tSv63

Anyone care to give it a try? I haven't made multi-value argument
test code for it yet but it works fine for single-arguments.

-Riskable
http://riskable.com
"Illogical beliefs cannot be undone by logic."

Sylvain

unread,
Oct 10, 2009, 3:26:04 AM10/10/09
to Tornado Web Server
Great, it works fine. I hope it will be added to the core project.

Now, we can improve self.request.arguments

In web.py/RequestHandler

add something like this :

@property
def arguments(self)
return dict((k, get_argument(k, allow_multiple=True)) for k in
self.request.arguments)

So if self.request.arguments = {'k1': ['unicode_val1'], 'k2':
['unicode_val21', 'unicode_val22']}
then
self.arguments should returns :
{'k1': u'unicode_val1', 'k2': [u'unicode_val21', u'unicode_val22']}

It's better I think.

Regards
> -Riskablehttp://riskable.com
Reply all
Reply to author
Forward
0 new messages