Thursday, March 26, 2015

Re: QueryDict and its .dict() method - why does it do that?

On 2015-03-26, at 18:48 , Gabriel Pugliese <gabrielhpugliese@gmail.com> wrote:
> This gist is self informative - some information from list is lost: https://gist.github.com/gabrielhpugliese/640b69eefc5b7490a07c
>
> Some of my buddies have pasted Rails(Rack) and PHP conversion right below. Is that something I am missing? Does it have to do with laziness?


Django (alongside just about every system which isn't PHP, from which
Rails lifted this "feature") doesn't give any semantic significance to
querystring key names. Keys are not parsed, and there is no magical
autovivification feature.

There is no information loss you haven't requested, QueryDict.dict() (or using a
QueryDict as a dictionary, there usually is not reason to reify it to a
native dict object) behaves as if you'd passed the querystring's (key,
value) pairs to a dict constructor: the last key wins.

If you expect a key to be present multiple times in a querystring, use
QueryDict.getlist(). If you want a native dict object, call
QueryDict.lists() and pass its result to dict().

Other Python web frameworks generally behave the same way, though their
interface may be different:
http://werkzeug.pocoo.org/docs/0.10/datastructures/#werkzeug.datastructures.MultiDict
http://bottlepy.org/docs/dev/api.html#bottle.MultiDict
http://docs.webob.org/en/latest/reference.html#query-post-variables
http://tornado.readthedocs.org/en/latest/web.html#tornado.web.RequestHandler.get_query_argument

On 2015-03-26, at 20:21 , Daniel Roseman <daniel@roseman.org.uk> wrote:
> The [] convention is not a standard part of HTTP. I believe it was developed in PHP and DHH adopted it for Rails, but that doesn't make it a standard. Django does not expect data in that format and does not do any specific conversion for it.

To be fair, URLs are not defined directly in HTTP's RFCs and urlencoded
querystrings are not part of the URL or URI RFCs either, which define
the query string as an almost arbitrary pile of stuff between ? and #
(though RFC 3986 does give URL examples using that pattern), it's part
of HTML: http://www.w3.org/TR/html5/forms.html#url-encoded-form-data

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/FC9F0113-0274-4BDF-A458-967A5E76CFB2%40masklinn.net.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment