Can't render json format data from views.py to django template to build DataTable

Can't render json format data from views.py to django template to build DataTable

spatan01spatan01 Posts: 2Questions: 1Answers: 0

Hi All,
Description of problem:
I have a Django project and I need to migrate DataTable there including child rows and so on.

I have followed the example below, built the table, sourced CSS, and js codes:

https://datatables.net/examples/api/row_details.html

But I faced the issue while rendering JSON format data from my views.py to Django template. In the page, I am getting an empty table and following warning message:

Error messages shown:
**DataTables warning: table id=table_id - Requested unknown parameter 'id' for row 0, column 1. For more information about this error, please see http://datatables.net/tn/4**

Debugger code:
https://debug.datatables.net/urewec

Link to test case:
It's run on my local server in Linux, so I Can't put the link, but I will show my codes

I have attached HTML, JS, views.py files

Could you please help me solve this issue?? I have investigated the cases from forums and looks like there is no such case there

Thanks

Answers

  • kthorngrenkthorngren Posts: 21,341Questions: 26Answers: 4,954
    edited April 2020

    I don't think "data": JSON.parse(JSON.stringify("{{ table }}")), is going to work. There seems to be a few things wrong with this statement. I've not tried rendering data into the Javascript portion of the page this way so I'm not sure how it would work. I use Flask not Django and with my pages I have a route that renders the HTML page then another route that handles fetching the data via Ajax. I would do something like this:

    from django.shortcuts import render, redirect, reverse, render_to_response
    from .models import rt_reg_res
    from django.http import HttpResponse, JsonResponse
    
    def units(request):
        """Return webpage"""
        return render(request, 'runtime.html')
    
    def get_units(request):
        """Return data form ajax request"""
        #. Datatables defaults looking for the data in the `data` object
        data  ={ "data":  [
        {
          "id": "1",
          "run_date": "2020/04/24",
          "ref_vs_new": "1.72562",
          "cts_suite": "rt_cts",
          "cts_avg": "-1.5",
          "cts_tavg": "7.6",
          "cts_testcase_count": "15 / 16",
        },
        ]
    }
        return JsonResponse(data)
    

    I think Django uses JsonResponse to return JSON data.

        var b2 = $('#table_id').DataTable( {
            "ajax": "<path to server>/get_units". // Fetch data from Django route
            "columns": [
                {
                   "className":      'details-control',
                   "orderable":      false,
                   "data":           null,
                   "defaultContent": ''
                },
                { "data": "id" },
                { "data": "run_date" },
                { "data": "ref_vs_new" }
            ],
            "order": [[1, 'asc']],
        } );
    

    You may need to fix the Python code as I'm not familiar with Django syntax and methods.

    Take a look at the Data Sources docs and Ajax docs for more details.

    Kevin

  • spatan01spatan01 Posts: 2Questions: 1Answers: 0

    Hi Kevin,

    Thank you so very much for the help, I have finally resolved the issue, and build the desired table :)

    I have followed Data Sources docs and get the solution there

    Once again, thank you, as your shared data helped me a lot

    Regards,
    Matso

This discussion has been closed.