【发布时间】:2021-02-16 16:59:04
【问题描述】:
我正在尝试将 25600 行加载到数据表中,但这大约需要 10 秒。该请求是通过 ajax API 调用。
views.py
@api_view()
def get_all_data(request):
get_all_data_ = Data.objects.values("name","contact_number","email_address","address","age",
"location_type","sector","phase","total_data","total_usage","city","district")
return JsonResponse(list(get_all_data_), safe=False)
模板.html
var table = $('#data-table').DataTable({
serverSide: true,
"ajax": {
"url": "/alldata/",
"dataSrc": ""
},
"columns": [
{"data": "name"},
{"data": "contact_number"},
{"data": "email_address"},
{"data": "address"},
{"data": "age"},
{"data": "location_type"},
{"data": "sector"},
{"data": "phase"},
{"data": "total_data"},
{"data": "total_usage"},
{"data": "city"},
{"data": "district"}
],
});
我怎样才能让它瞬间完成?
【问题讨论】:
-
这并不奇怪。 Python 处理不了那么快。即使没有查询,仅处理
list(range(25600))也很容易花费大约 2-5 秒。所以你没有。通常,无论如何您都不需要(立即)HTML 页面中的所有数据。您应该对其进行分页,并让 JavaScript 仅在必要时加载块。无论如何,渲染 25600 也会对浏览器产生影响。
标签: django django-rest-framework django-templates django-datatable