techie11
techie11

Reputation: 1387

Django with jQuery/datatable

I was assigned with a task which required me to understand the following Python code which uses jQuery and datatable:

req = request.GET
sort_col = req['columns[%s][data]' % req['order[0][column]']]
glob_search_val = req['search[value]']
if req['order[0][dir]'] != 'asc':

I'm new to jQuery and datatables. The usage is quite different from how normal Python lists/dictionaries are accessed. For example, order[0][column] apparently is a variable but it's quoted which means a literal in Python. the column in the bracket appears to be a variable, it's again inside quotes and not initialized. In datables documentation order is an 2D array access by index like order[0][0]. I have similar question for data in columns[%s][data] and what does columns[%s][data] return?

Where can I find more information on how columns, order, search (and other) parameters work in Django?

Upvotes: 0

Views: 486

Answers (1)

Foot
Foot

Reputation: 449

Those parameter names you reference (sort, columns, search) are just keys in a request.GET QueryDict. There's nothing intrinsic to Django about those parameter names in a GET request (i.e. nothing magical happens in Django with those parameter key names).

The %s and %s are part of old-style python string formatting, so the %s will be replaced by whatever is in req['order[0][column]] which will then store the sort column field name in sort_col.

As far as actual filtering/searching/ordering etc in Django, that's through the Django QuerySet API. Presumably you will be sorting/ordering/searching a list of objects stored in a Database and defined in whatever Model was displayed in your Datatable e.g. FooModel.objects.filter(some_field=search_val).order_by(sort_col)

Upvotes: 1

Related Questions