【问题标题】:Datatables with Django can not fetch data from MySQL Database使用 Django 的数据表无法从 MySQL 数据库中获取数据
【发布时间】: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 实现排序、搜索和分页。

【问题讨论】:

标签: jquery django datatables


【解决方案1】:

首先,您不需要在 HTML 中明确指定列定义。您只需要&lt;table id="example"&gt;&lt;/table&gt;

另外,您必须注意,由于您将“dataSrc”设置为空字符串,您的客户端需要来自服务器的条目数组。

因此,您可能希望使用浏览器的开发人员工具控制台检查从服务器接收到的可能错误和数据格式,以解决问题。

【讨论】:

    【解决方案2】:

    for循环应该在tbody以上

    将您的代码更改为如下所示:

    {% for foo in xh %}
    <tbody>
        <tr>
            <td>{{ foo.laptops_name}}</td>
            <td>{{ foo.brand_name}}</td>
            <td>{{ foo.laptops_price}}</td>
        </tr>
    </tbody>
    {% endfor %}
    

    【讨论】:

      猜你喜欢
      • 2020-02-08
      • 2018-11-24
      • 2021-12-20
      • 2021-09-18
      • 2020-11-16
      • 1970-01-01
      • 2015-10-05
      • 2023-04-05
      • 1970-01-01
      相关资源
      最近更新 更多