【发布时间】:2019-06-16 19:33:56
【问题描述】:
我正在尝试通过使用 Django 在datatables 提供的默认搜索栏中进行搜索来获取数据。显示默认视图,但是当我在搜索框中搜索时,它什么也没显示。它显示的视图如下:
我的观点是这样的:
def searchProductTable(request):
xh = ProductLaptop.objects.all()
context = {
"xh": xh
}
return render(request, "searchTableView.html", context)
对于以下型号:
class ProductLaptop(models.Model):
laptops_name = models.CharField(max_length=500, null=True, blank=True)
laptops_url = models.CharField(max_length=500, null=True, blank=True)
laptops_price = models.IntegerField(null=True, blank=True)
laptops_image = models.CharField(max_length=400, null=True, blank=True)
brand_name = models.CharField(max_length=20, null=True, blank=True)
def __str__(self):
return self.laptops_name
我知道模型不正确,同时我不知道如何实现模型,因为我对 Django 没有太多经验。
我的.html文件的代码是:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Search Table</title>
<link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet">
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
</head>
<body>
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Seller</th>
<th>Price</th>
</tr>
</thead>
<tbody>
{% for foo in xh %}
<tr>
<td>{{ foo.laptops_name}}</td>
<td>{{ foo.brand_name}}</td>
<td>{{ foo.laptops_price}}</td>
{% endfor %}
</tr>
</tbody>
</table>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script>
$('#example').dataTable({
ajax: {
url: 'http://127.0.0.1:800/searchtable/',
dataSrc: ''
},
columns: [
{ "data": "laptops_name"},
{ "data": "brand_name"},
{ "data": "laptops_price"},
]
});
</script>
</body>
</html>
谁能帮我解决这个问题。在过去的两天里,我一直在尝试使用 DataTable 实现排序、搜索和分页。
【问题讨论】:
-
不是数据表专家,但也许你可以看看这个:pypi.org/project/django-datatable
标签: jquery django datatables