DataTables in Django application - JQuery does not run to format table

DataTables in Django application - JQuery does not run to format table

PayrollXLPayrollXL Posts: 2Questions: 1Answers: 1

Description of problem:
I am trying to implement DataTables in a **Python Django **application. I have the correct references to the DataTables CSS and JavaScript, and I also have JQuery version 3.7.1 included.

However, when the page finishes loading, no formatting is done. If I open the console and call $('#tablename).DataTable() the formatting does take place.

I have the script included at the bottom of the HTML page as follows...

{% block javascript %}

$(function() {
$("depttable").DataTable();
}

{% endblock javascript %}

If I paste that code directly into the console, the DataTable() process runs, and the HTML table is formatted. This tells me that the inclusion of both JQuery and the DataTables CSS and JS is correct.

This doesn't seem to have anything to do with DataTables() itself, but I'm trying to figure out why the code is not executing. It is literally the last section in the HTML, so the table should definitely have been rendered when it gets to this point.

If anyone has experience using DataTables() with Django, any help on this would be greatly appreciated. I think DataTables will be a huge addition to this project, I just need to get past this first hurdle!

Thanks in advance!
Bob

This question has an accepted answers - jump to answer

Answers

  • PayrollXLPayrollXL Posts: 2Questions: 1Answers: 1
    Answer ✓

    Okay, I was actually able to figure this out, and the solution was pretty simple. I just needed to include the call to DataTable() in a script block itself instead of placing it between {% block javascript%} and {% endblock javascript %}.

    It's working now!!

Sign In or Register to comment.