Thursday, July 24, 2014

Django request.GET and jQuery Datatables

Dear all,

I'm trying to let Django being able to answer to requests by jQuery-Datatables (http://datatables.net/).
Considering the example at this link (http://www.datatables.net/examples/data_sources/server_side.html) to fill up the jQuery-Datatables table with data provided by the server, a typical request has the following form:

    http://www.datatables.net/examples/server_side/scripts/server_processing.php?draw=2&columns%5B0%5D%5Bdata%5D=0&columns%5B0%5D%5Bname%5D=&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=true&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B1%5D%5Bdata%5D=1&columns%5B1%5D%5Bname%5D=&columns%5B1%5D%5Bsearchable%5D=true&columns%5B1%5D%5Borderable%5D=true&columns%5B1%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B1%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B2%5D%5Bdata%5D=2&columns%5B2%5D%5Bname%5D=&columns%5B2%5D%5Bsearchable%5D=true&columns%5B2%5D%5Borderable%5D=true&columns%5B2%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B2%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B3%5D%5Bdata%5D=3&columns%5B3%5D%5Bname%5D=&columns%5B3%5D%5Bsearchable%5D=true&columns%5B3%5D%5Borderable%5D=true&columns%5B3%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B3%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B4%5D%5Bdata%5D=4&columns%5B4%5D%5Bname%5D=&columns%5B4%5D%5Bsearchable%5D=true&columns%5B4%5D%5Borderable%5D=true&columns%5B4%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B4%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B5%5D%5Bdata%5D=5&columns%5B5%5D%5Bname%5D=&columns%5B5%5D%5Bsearchable%5D=true&columns%5B5%5D%5Borderable%5D=true&columns%5B5%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B5%5D%5Bsearch%5D%5Bregex%5D=false&order%5B0%5D%5Bcolumn%5D=0&order%5B0%5D%5Bdir%5D=asc&start=10&length=10&search%5Bvalue%5D=&search%5Bregex%5D=false&_=1406235319463

The problem, as I understand it, is basically that instead of pairs like "a=b", I get "a[0][x]=b".
Which is the best/commonly used way to deal with this kind of data?

With the legacy Datatables (http://legacy.datatables.net/usage/server-side), I could easily deal with the request, by creating a form like the following and filling it with the request.GET data, to easily handle the validation and casting.

    class DataTablesForm(forms.Form):
        iDisplayStart = forms.IntegerField(min_value=0)
        iDisplayLength = forms.IntegerField(min_value=-1)
        iSortingCols = forms.IntegerField(min_value=0, max_value=3)
        mDataProp_0 = forms.CharField()
        mDataProp_1 = forms.CharField()
        mDataProp_2 = forms.CharField()
        iSortCol_0 = forms.IntegerField(required=False, min_value=0, max_value=5)
        iSortCol_1 = forms.IntegerField(required=False, min_value=0, max_value=5)
        iSortCol_2 = forms.IntegerField(required=False, min_value=0, max_value=5)
        sSortDir_0 = forms.ChoiceField(required=False, choices=(('asc', 'asc'), ('desc', 'desc')))
        sSortDir_1 = forms.ChoiceField(required=False, choices=(('asc', 'asc'), ('desc', 'desc')))
        sSortDir_2 = forms.ChoiceField(required=False, choices=(('asc', 'asc'), ('desc', 'desc')))
        sEcho = forms.CharField(required=False)

and

    input_form = DataTablesForm(request.GET)

How do I deal though with the new format, i.e. "columns[i].name" instead of "mDataProp_(int)"?

I think the problem is how to deal with JSON formatted GET data within Django, if "JSON formatted GET data" makes any sense.

Thanks for your help,

Andrea

P.S. I've posted the same question on Stack Overflow: http://stackoverflow.com/questions/24943942/django-request-get-and-jquery-datatables

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

No comments:

Post a Comment