Why my records in datatables doesn't want to display?

Why my records in datatables doesn't want to display?

developer96developer96 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" },
                            ]
                        } )
                        
                    })
                    } ); 

Answers

  • tangerinetangerine Posts: 3,348Questions: 36Answers: 394

    The link in the error message is provided for your benefit. It explains diagnostic steps you should follow.

  • developer96developer96 Posts: 11Questions: 4Answers: 1
    edited January 2021

    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)))

  • developer96developer96 Posts: 11Questions: 4Answers: 1

    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

    def displayPatientsToFrontend(request):
    
        patients = Patient.objects.all()
        patients_json = serializers.serialize('json', patients)
        return JsonResponse({'patients_json': patients_json})
    
  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    Looks like you are creating JSON data with this:

      patients_json = serializers.serialize('json', patients)
    

    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

  • developer96developer96 Posts: 11Questions: 4Answers: 1

    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.

This discussion has been closed.