使用datatable首先須要下載datatable文件,文件主要包括三個文件夾css,img,js相關文件,在django中如何配置呢?css
首先須要在模板中引入datatable文件,格式以下:jquery
<!-- DataTables CSS --> <link rel="stylesheet" href="{% static 'DataTables/css/jquery.dataTables.css' %}"> <!-- jquery --> <script src="{% static 'js/jquery/jquery.min.js' %}"></script> <!-- DataTables --> <script src="{% static 'DataTables/js/jquery.dataTables.js' %}"></script>
注意datatables中的js文件須要引用在jquery文件以後.django
那麼django中如何讓數據使用datatables表格呢?spa
代碼以下:code
<table id="table_id" class="display"> <thead> <tr> <th>Column 1</th> <th>Column 2</th> </tr> </thead> <tbody> {% for d in data %} <tr> <td>{{ d.col1 }}</td> <td>{{ d.col2 }}</td> </tr> {% endfor %} </tbody> </table> <script></script>
$(document).ready( function () {
$('#table_id').DataTable();
} );
完畢!blog