Why my records in datatables doesn't want to display?
Why my records in datatables doesn't want to display?
developer96
Posts: 11Questions: 4Answers: 1
Hi, I'm trying to display records from backend(django) to the frontend(jQuery) with datatables. In DevTools I just got the object I want to render, but nothing is displayed. I'm stucking on this for a while, will be appreciate if someone say what I'm missing. When I render the endpoint I got message:
DataTables warning: table id=datatable - Requested unknown parameter 'coreapi_id' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4
Here is my code:
$(document).ready(function() {
var data;
fetch("http://192.168.2.85:8000/displayPatientsToFrontend/")
.then(response => response.json())
.then(json => data = json)
.then(() => {console.log(data);
$('#datatable').DataTable( {
data: data.patients_json,
deferRender: true,
scrollY: false,
scrollX: false,
scrollCollapse: true,
scroller: true,
columns: [
{ data: "coreapi_id" },
{ data: "first_name" },
{ data: "last_name" },
{ data: "email" },
{ data: "date_arrival" },
{ data: "date_departure" },
]
} )
})
} );
This discussion has been closed.
Answers
The link in the error message is provided for your benefit. It explains diagnostic steps you should follow.
I checked the link and followed the instructions. On Check for common issues I got: 15 tests complete. No failures or warnings found!
That's why I asked in the forum)))
My backend is django and this is what my function looks like, I just get all the records from the database, tried to print in terminal successfully, they just don't show in datatables
Looks like you are creating JSON data with this:
Then taking that JSON data and encoding it again into a JSON response. If you look at the response using the browser's network inspector you will probably see a bunch of unexpected backslashes due to JSON encoding the data twice. You only want to encode once.
If this doesn't help then please post the JSON response from the browsers network inspector so we can see what is being received.
Kevin
Thank you Kevin, I just found the "solution", I need to parse JSON in my jQuery too, and then my patient_json is in "fields" object from database, to access database fields in my data I need to write fields. + the name of the field I need. Maybe there is a better way to achieve this without writing "fields" but I found this works for now.