I wish I’d known this a long time ago. Django’s request object includes a dictionary of key/value pairs passed into the request via POST or GET methods. That dictionary, however, works in a counter-intuitive fashion. If a URL reads http://foo.com?a=boo, then the expected content of request.GET['a'] would be 'boo', right? And most of us who’ve used other URL parsers in the past know that http://foo.com?a=boo&a=hoo know that the expected content of request.GET['a'] would be ['boo', 'hoo'].
Except it isn’t. It’s just 'hoo'. Digging into the source code, I learn that in Django’s MultiValueDict, __getitem__(self, key) has been redefined to return the last item of the list. I have no idea why. Maybe they wanted to ensure that a scalar was always returned. The way to get the whole list (necessary when doing an ‘in’ request) is to call request.GET.getlist('a').
Lesson learned, an hour wasted.
This entry was automatically cross-posted from Elf's technical journal, ElfSternberg.com
no subject
Date: 2010-10-24 07:27 pm (UTC)no subject
Date: 2010-10-25 05:19 am (UTC)no subject
Date: 2010-10-24 09:46 pm (UTC)no subject
Date: 2010-10-25 10:39 am (UTC)no subject
Date: 2010-10-25 01:20 pm (UTC)no subject
Date: 2010-10-25 02:52 pm (UTC)And seperately, why returning the last instead of the first value is more sensible?
no subject
Date: 2010-10-25 05:05 pm (UTC)no subject
Date: 2010-10-25 06:19 pm (UTC)no subject
Date: 2010-10-25 11:14 pm (UTC)<select name="foo" multiple>form element.no subject
Date: 2010-10-26 01:23 am (UTC)no subject
Date: 2010-10-25 11:59 pm (UTC)no subject
Date: 2010-10-26 01:22 am (UTC)no subject
Date: 2010-10-27 05:31 pm (UTC)RFC 1866: Hypertext Markup Language - 2.0 from November 1995. The example form submission contains exactly what we're talking about here. This feature remains in all newer versions, including the latest HTML 5 draft.
no subject
Date: 2010-10-27 05:59 pm (UTC)Frank appears to be confusing a common (but not universal) operation of the "equals" symbol as found in many (but not all) programming languages (see: Scheme, Pascal, Erlang, among others) with section 8.2.4's specialized (but linguistically acceptible) compromise with respect to the tokenization of CGI arguments.
no subject
Date: 2010-10-29 03:28 pm (UTC)