Friday, February 26, 2016

Re: Parsing PHP-style GET parameters for lists and dicts in Django

On Fri, Feb 26, 2016 at 2:49 AM, Remco Gerlich <remco@gerlich.nl> wrote:
Hi James,

I agree with the general dirtyness of PHP and I have used Django exclusively for years.


Ah, the way you posed the question I took you to be a recent convert. Sorry about that. ;-)
 

The type of parameter I mean is where, say a list of two elements [1,2] is sent as

So instead of two parameters with the same name, this has two parameters with different names ("list[0]" and "list[1]"). PHP automatically recognizes such parameters and turns them into an list under the name "list", I seem to recall vaguely. The same works for dictionaries and for nested lists and dictionaries. I don't think Django does this and I don't see anything about it in the docs you linked to.

I can use [request.GET.get("list[0]"), request.GET.get("list[1]")] etc but the structure can get quite involved ("columns[2][search][regex]") and so I was wondering if someone had written something like that already.


I vaguely recall this being PHP-specific back in my Drupal days, and thought it was a terrible idea then, so you may be right. Looks like this SO may cover it though: http://stackoverflow.com/questions/5170087/reading-multidimensional-arrays-from-a-post-request-in-django

That SO links to this thing: https://github.com/bernii/querystring-parser

Looks like it might be what you're looking for.

-James

--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciWTBH3EJUaQyn3%2B%2BA4QxT788pROnFkpdCMgXaCzkMQYiw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment