使用Datatable的服务器端处理Django模板

时间:2015-06-04 17:44:53

标签: jquery python ajax django datatables

我一直在Django实现上使用DataTables。我的网页上显示的表格最近变得足够大,我需要启用server side processing来减轻客户端的负担。我无法让DataTables与服务器端处理一起工作。

urls.py:

urlpatterns = patterns('',
    url(r'^get_customers$', views.get_customers, name = "get_customers"),
)

的观点:吡啶

def get_customers(request):
    json_test = json.dumps({                   
        "draw": 1,
        "recordsTotal": 1,
        "recordsFiltered": 1,
        "data": [         
                "test",
                "test",                   
                "test",                     
                "test"                           
    ]})
    return HttpResponse(
                    json_test,
                    content_type="application/json"
    )

模板:

        <table id="table_id" class="table table-striped"
            data-url="{% url 'dashboard:get_customers' %}">
          <thead>
            <tr>
                {% block table_head %}
                <th>Customer</th>
                <th>Devices</th>
                <th>Score (%)</th>
                <th>Status</th>
                {% endblock table_head %}
            </tr>
          </thead>
          <tbody>
          </tbody>
      </table>

jquery.data表-init.js:

$(document).ready( function () {
    $('#table_id').DataTable({
                          "processing": true,
                          "serverSide": true,
                          "ajax": $(this).attr('data-url'),
     });
});

我从manage.py获得此输出:

[04/Jun/2015 14:07:20] "GET /dashboard/ HTTP/1.1" 200 9328
[04/Jun/2015 14:07:20] "GET /static/dashboard/css/base.css HTTP/1.1" 304 0
[04/Jun/2015 14:07:20] "GET /static/dashboard/bootstrap-3.3.4-dist/css  /bootstrap.min.css HTTP/1.1" 304 0
[04/Jun/2015 14:07:20] "GET /static/dashboard/js/jquery-1.11.3.min.js HTTP/1.1" 304 0
[04/Jun/2015 14:07:20] "GET /static/dashboard/bootstrap-3.3.4-dist/js/bootstrap.min.js HTTP/1.1" 304 0
[04/Jun/2015 14:07:20] "GET /static/dashboard/DataTables-1.10.2/media/js/jquery.dataTables.min.js HTTP/1.1" 304 0
[04/Jun/2015 14:07:20] "GET /static/dashboard/js/jquery.data-table-init.js HTTP/1.1" 304 0
[04/Jun/2015 14:07:20] "GET /static/dashboard/js/jquery.csv-export.js HTTP/1.1" 304 0
[04/Jun/2015 14:07:20] "GET /dashboard/?draw=1&columns%5B0%5D%5Bdata%5D=0&columns%5B0%5D%5Bname%5D=&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=true&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B1%5D%5Bdata%5D=1&columns%5B1%5D%5Bname%5D=&columns%5B1%5D%5Bsearchable%5D=true&columns%5B1%5D%5Borderable%5D=true&columns%5B1%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B1%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B2%5D%5Bdata%5D=2&columns%5B2%5D%5Bname%5D=&columns%5B2%5D%5Bsearchable%5D=true&columns%5B2%5D%5Borderable%5D=true&columns%5B2%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B2%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B3%5D%5Bdata%5D=3&columns%5B3%5D%5Bname%5D=&columns%5B3%5D%5Bsearchable%5D=true&columns%5B3%5D%5Borderable%5D=true&columns%5B3%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B3%5D%5Bsearch%5D%5Bregex%5D=false&order%5B0%5D%5Bcolumn%5D=0&order%5B0%5D%5Bdir%5D=asc&start=0&length=10&search%5Bvalue%5D=&search%5Bregex%5D=false&_=1433437640548 HTTP/1.1" 200 9328

除了那个输出。 Mozilla Firefox的网络控制台告诉我有一个TypeError: g is null,数据表提供以下输出:DataTables warning: table id=table_id - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/

我觉得我错过了一个步骤,但在DataTables文档,一些教程和其他类似的堆栈溢出问题之间,我似乎无法弄清楚它可能是什么。

0 个答案:

没有答案