DataTables doesn't detect properly JSON?
DataTables doesn't detect properly JSON?
I'm currently trying to show a Table with DataTable, and all the time keeps saying "No matching records found" I'm using Django, and i have my JSON on a page from the Django, that's how i get the Ajax, and with python i put the data of a local database and show it with json module...
Javascript:
$(document).ready(function() {
$('#table).DataTable({
serverSide: true,
paging: true,
pageLength: 100,
ajax: {
url: 'X',
dataSrc: 'data'
},
deferRender: true,
columns: [
{ data: 'x'},
{ data: 'x'},
{ data: 'x'},
{ data: 'x'},
{ data: 'x'},
{ data: 'x'},
{ data: 'x'},
{ data: 'x'},
{ data: 'x'},
{ data: 'x'},
{ data: 'x'},
{ data: 'x'},
{ data: 'x'},
{ data: 'x'},
{ data: 'x'},
{ data: 'x'},
{ data: 'x'},
{ data: 'x'},
{ data: 'x'},
{ data: 'x'},
{ data: 'x'},
{ data: 'x'},
{ data: 'x'},
{ data: 'x'},
{ data: 'x'},
{ data: 'x'},
]
});
});
And my Json:
"{\"draw\": 2, \"recordsTotal\": 4, \"recordsFiltered\": 4, \"data\": [{\"x\": \"x\", \"bastidor\": \"x\", \"x\": \"x\", \"x\": \"x\", \"x\": \"x\", \"x\": \"x\", \"x\": x, \"x\": \"x\", \"x\": x, \"x\": null, \"x\": \"x\", \"x\": \"x\", \"x\": x, \"x\": x, \"x\": x, \"x\": \"x\", \"x\": \"x\", \"x\": \"x\", \"x\": \"x\", \"x\": \"x\", \"x\": \"x\", \"x\": \"None\", \"x\": \"x\", \"x\": \"x\", \"x\": null, \"x\": \"\"}]}
The ones without "x" are integers or floats and the one in the end doesnt have anything
What do you think? I use special characters and i think that could be the problem, because JSON doesn't detect it well, but i validate it and it says it doesn't have a problem i mean like i dont only use string as you can see...
Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be found in this guide
This question has an accepted answers - jump to answer
Answers
The JSON string has escaped double quotes (
\"
). This suggests you are JSON encapsulating the data twice in your Django App. Once this is fixed you can remove theajax.dataSrc
option asdata
is the default. It won't hurt to leave it though.Kevin
My Django code:
```def mostrar_JSON(request):
start = int(request.GET.get('start', 0))
length = int(request.GET.get('length', 100))
I don't know how to fix it, maybe the error is in the DataBase?
In line 68 you are serializing
response_data
into JSON. Then in line 71 you are serializing the data again into a JSON string. I think you can remove 68-70 and just use theJsonResponse
method to serializeresponse_data
.Kevin
Your Python code is a bit confusing. I guess you are changing the variables and other data with just
x
to post on the forum.The idea with server side processing is to create a SQL query that only gets the records to be displayed on the page. It looks like you are fetching all the records from the DB then using
x = x[start:start + length]
to just get the records for the page. To keep from fetching all the rows use SQL LIMIT and OFFSET to fetch only the rows from the page.Maybe you don't need server side processing enabled. Do you have many thousands of rows that cause performance issues? If not you can disable server side processing, to return all rows, and let sorting, searching and paging happen at the client. You won't need to calculate and return the
draw
,recordsTotal
andrecordsFiltered
values.It doesn't look like you are calculating the
recordsTotal
andrecordsFiltered
values correctly.recordsTotal
should be the total number of records in the DB.recordsFiltered
should be the total number of records after filtering. The above code is setting these to the number of records returned. This will result in the Datatables thinking you have only one page even if you have more than 100 records (pageLength: 100,
). See the (server side processing protocol docs](https://datatables.net/manual/server-side) for details.It looks like your SQL query doesn't support the sorting and searching capabilities from the client side Datatable. See the server side processing protocol docs for details. There are third party Django libraries that support server side processing. I haven't used any but, if you need server sid eprocessing, you might want to take a look to see what's available. For example:
https://pypi.org/project/django-serverside-datatable/
Kevin