Таблица данных на стороне сервера Dhnago с помощью DataTable.net
Я пытаюсь реализовать таблицу данных на стороне сервера с помощью Django. Мои данные поступают на сторону клиента с сервера (как я проверил console.log(data)), но они не отображаются в таблице. Если кто-то из вас может помочь мне с этим, я буду очень благодарен. Заранее спасибо!
Шаблон:
<div class="container">
<div class="row">
<div class="col-md-12">
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>Uploader</th>
<th>Main Category</th>
<th>Sub Category</th>
<th>Product Name</th>
<th>Product Price</th>
<th>Brand Name</th>
<th>In Stock</th>
<th>Product Number</th>
<th>Warranty</th>
<th>Uploaded</th>
<th>Image</th>
<!-- <th>Action</th> -->
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
Script:
$(document).ready(function () {
$('#example').DataTable({
responsive: true,
"serverSide": true,
"processing": true,
"ajax": {
url: '{% url "mainTableData" %}',
success: function(data){
console.log(data)
},
error: function (error){
console.error(error)
}
},
});
});
Views.py:
def mainTableData(request):
ajax_response = {}
search_values = []
fields = ['v_user__username', 'v_main_category__cat_name', 'v_sub_category__sub_cat',
'v_product_name', 'v_product_price', 'v_brand_name',
'v_in_stock', 'v_product_number', 'v_product_warranty',
'v_created'
]
for i in range(1, 10):
value = request.GET.get('columns['+str(i)+'][search][value]')
search_values.append(value)
products = VendorProduct.objects.filter(reduce(AND, (Q(**{fields[i]+'__icontains': value} ) for i, value in enumerate(search_values)))).values_list('v_user__username', 'v_main_category__cat_name', 'v_sub_category__sub_cat',
'v_product_name', 'v_product_price', 'v_brand_name',
'v_in_stock', 'v_product_number', 'v_product_warranty',
'v_created').order_by('-id')
# Add paginator
paginator = Paginator(products, request.GET.get('page_length', 3))
showing_rows_in_current_draw = request.GET.get('length')
products_list = paginator.get_page(showing_rows_in_current_draw)
data = list(products_list)
ajax_response['draw'] = 1
ajax_response['recordsTotal'] = len(products)
ajax_response['recordsFiltered'] = 3
ajax_response['data'] = data
return JsonResponse(ajax_response, safe=False)