myapp/
-
-
Save oisobstudio/b99d568974a3393865d53d85cad04106 to your computer and use it in GitHub Desktop.
this is awesome trick for datatables server side on django
sorry please correct the js client code if type POST request
"ajax": {
"url": "{% url 'app_url:ajax_web_list' %}",
"type": "post",
"data": {
"csrfmiddlewaretoken": "{{ csrf_token }}"
}
}"ajax": {
"url": "{% url 'app_url:ajax_web_list' %}",
"type": "get",
}Where you use start variable?
Is this working perfectly?
Does not work for me!
Pagination it's wrong, you need to change to this:
page_number = start / length + 1
try:
object_list = paginator.page(page_number).object_list
except PageNotAnInteger:
object_list = paginator.page(1).object_list
except EmptyPage:
object_list = paginator.page(1).object_list
Thank you for coding of datatables with Django.
I run to above code . But line of "draw = int(datatables.get('draw')) " take following errors.
"TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'"
hi, thanks for the code, it works after applied the solution in comment sections (also thanks to contributor) , it really help me!
btw, the sorting does not work , so i add these lines :
- Ambil data untuk ordering
`
order_idx = int(datatables.get('order[0][column]'))
order_dir = datatables.get('order[0][dir]')
order_col = 'columns[' + str(order_idx) + '][data]'
order_col_name = datatables.get(order_col)
if (order_dir == "desc"):
order_col_name = str('-' + order_col_name)
... (code filtering data)
- after data filterred , apply order
datas = datas.order_by(order_col_name)
it's just there is still this bug, the data in first row is not sorted. still try to figure out the solutions
update, i found this column filtering in datatables but it's for client side rendering ( https://datatables.net/extensions/fixedheader/examples/options/columnFiltering.html )
so i added it to able using server side, cont from code above
idx=0
for itemcol in datatables:
colname = 'columns['+str(idx)+'][data]'
colsearchval = 'columns['+str(idx)+'][search][value]'
colname = datatables.get(colname)
colsearchval = datatables.get(colsearchval)
if (colsearchval):
search_type = 'icontains'
filter = colname + '__' + search_type
datas=datas.filter(**{ filter: colsearchval })
records_total = datas.count()
records_filtered = records_total
idx+=1
Works in parts!