Friday, February 26, 2016

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

Hi James,

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

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.

Greetings,
Remco


On Fri, Feb 26, 2016 at 11:24 AM, James Schneider <jrschneider83@gmail.com> wrote:


On Feb 26, 2016 1:21 AM, "Remco Gerlich" <remco@gerlich.nl> wrote:
>
> I need to work with DataTables, a jQuery-plugin for sortable HTML tables.
>
> It sends Ajax GET requests with parameters like the following (but urlencoded, of course):
>
> columns[0][data]=0
> columns[0][name]=
> columns[0][searchable]=true
> columns[0][orderable]=true
> columns[0][search][value]=
> columns[0][search][regex]=false
> columns[1][data]=1
> columns[1][name]=
> columns[1][searchable]=true
> columns[1][orderable]=true
> columns[1][search][value]=
> columns[1][search][regex]=false
> columns[2][data]=2
> columns[2][name]=
> columns[2][searchable]=true
> columns[2][orderable]=true
> columns[2][search][value]=
> columns[2][search][regex]=false
>
> I think this plugin was written with PHP in mind, and that PHP can automatically convert that into a list of dicts that would in Python look something like

PHP is a dirty word in this forum. :-P

>
> columns = [
>     {"data": 0, "name": '', "searchable": True, "orderable": True, "search": {"value": '', "regex": False}},
>     {"data": 1, "name": '', "searchable": True, "orderable": True, "search": {"value": '', "regex": False}},
>     {"data": 2, "name": '', "searchable": True, "orderable": True, "search": {"value": '', "regex": False}},
> ]
>
> Does anybody know of any Python or Django app / library that does this?

Easy. It's called Django. :-D

The HttpRequest object is automatically populated with both GET and POST values when Django parses the request from the browser, and handles multiple values for the same key just fine.

https://docs.djangoproject.com/en/1.9/ref/request-response/#django.http.HttpRequest.GET

Usually this object is made available as a variable called request in a function-based view, or is available via self.request in a class-based view.

To get multiple values for the same key, simply access that key multiple times (see the QueryDict link in the section I linked above), usually as part of a loop. The QueryDict (request.GET) acts like a Python generator.

Usually you access the keys using .get() calls so that a missing or empty key return None instead of raising an exception:

fn = self.request.GET.get('first_name')

This part of the tutorial has example usage: https://docs.djangoproject.com/en/1.9/intro/tutorial04/#write-a-simple-form

It uses request.POST, but it works exactly the same way.

On a side note, nothing you've mentioned is specific to PHP, rather it is all part of the HTTP protocol. The languages/frameworks just have different methods of accessing the same information. A form generated by a PHP process can submit to a Django view with no issue, and vice versa.

-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%2BciVN%3DBNzapoBtv9kEO2c-e85S759WmYWR_YVaeXid8B9bQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
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/CAFAGLK3KSRwWC2P8xXRhEOtXFCvpW7o0mUqJZiamaaBRDme6ew%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment