数据表未在 django-datatable-view 中填充数据 [英] Data table not populating data in django-datatable-view

查看:21
本文介绍了数据表未在 django-datatable-view 中填充数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始了一个新的 Django 项目,试图测试 django-数据表视图.

我收到一个 JS 错误,说 Uncaught TypeError: $$.each is not a function.尽管遵循图书馆网站上的代码 jQuery 是在 datatableview.js 之前加载的.

models.py

from django.db 导入模型类邮政(模型.模型):标题=models.CharField(max_length=150)body = models.TextField()created = models.DateField()

views.py

from datatableview.views 导入 DatatableView从 .models 导入帖子类 MyView(DatatableView):模型 = 帖子数据表选项 = {'列': ['标题','身体','创造',]}

urls.py

from django.urls 导入路径从 .导入视图网址模式 = [path('', views.MyView.as_view(), name='myview'),]

post_list.html

{% 加载静态 %}<!-- myapp/mymodel_list.html --><!-- 加载依赖项--><script src="https://code.jquery.com/jquery-3.3.1.min.js"完整性="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="crossorigin="anonymous"></script><link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css"><script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script><!-- 通过服务器端选项加载用于初始化表的 js --><script type="text/javascript" charset="utf8" src="{% static 'js/datatableview.js' %}"></script><script type="text/javascript">$(函数(){datatableview.initialize('.datatable');});<!-- 渲染表骨架,包括准备就绪初始化程序的 .datatable 类.-->{{ 数据表 }}

控制台错误:

 query-3.3.1.min.js:2 jQuery.Deferred 异常:$$.each 不是函数类型错误:$$.each 不是函数在 Object.initialize(http://127.0.0.1:8000/static/js/datatableview.js:20:12)在 HTMLDocument.<匿名>(http://127.0.0.1:8000/:17:23)在 l (https://code.jquery.com/jquery-3.3.1.min.js:2:29375)在 c (https://code.jquery.com/jquery-3.3.1.min.js:2:29677)不明确的w.Deferred.exceptionHook @ jquery-3.3.1.min.js:2c @ jquery-3.3.1.min.js:2设置超时(异步)(匿名)@ jquery-3.3.1.min.js:2你@jquery-3.3.1.min.js:2fireWith @ jquery-3.3.1.min.js:2火@jquery-3.3.1.min.js:2你@jquery-3.3.1.min.js:2fireWith @ jquery-3.3.1.min.js:2准备好了@ jquery-3.3.1.min.js:2_ @ jquery-3.3.1.min.js:2jquery-3.3.1.min.js:2 Uncaught TypeError: $$.each 不是函数在 Object.initialize (datatableview.js:20)在 HTMLDocument.<匿名>((索引):17)在 l (jquery-3.3.1.min.js:2)在 c (jquery-3.3.1.min.js:2)

仅呈现表头,而未填充数据.关于可能发生的事情的任何想法.正如我所说,这里的大部分答案都提到 jquery 没有首先加载,但这显然不是上面代码中发生的事情.

解决方案

我遇到了完全相同的问题.所以代替

我们必须初始化数据表:

在你的 views.py 中,而不是

class MyDatatableView(DatatableView):模型 = 收入列 = [标题",正文",创建"]search_fields = [标题",正文"]

您必须这样做(使用 DatatableView 中的模型或 query_set:

class MyDatatable(Datatable):元类:列 = [标题",正文",创建"]search_fields = [标题",正文"]类 MyDatatableView(DatatableView):模型 = 收入datatable_class = MyDatatable

但是后来我得到了以下 js essor,知道吗?我正在使用 jQuery 3.3.1 和这个版本的数据表:http://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js

未捕获的类型错误:datatable.api 不是函数在 HTMLDocument.<匿名>(数据表:187)在 c (jquery.min.js:3)在 Object.fireWith [as resolveWith] (jquery.min.js:3)在 Function.ready (jquery.min.js:3)在 HTMLDocument.H (jquery.min.js:3)

刚刚找到原因,api调用必须是api而不是api(),因为datatableview.js中加了()

var table = datatable.api;

我的新问题是搜索,它返回 500 服务器错误 wichi 是 "FieldError('Related Field got invalid lookup: {}'.format(lookup_name))"但是,即使我添加了我想要搜索的列,如title__name",我仍然在搜索中收到警告:

DataTables 警告:table id=DataTables_Table_0 - Ajax 错误.有关此错误的详细信息,请参阅http://datatables.net/tn/7>

I've started a new Django project with trying to test out django-datatable-view.

I'm getting a JS error saying Uncaught TypeError: $$.each is not a function. Although following the code on the library's website jQuery is being loaded before datatableview.js.

models.py

from django.db import models
class Post(models.Model):
    title= models.CharField(max_length=150)
    body = models.TextField()
    created = models.DateField()  

views.py

from datatableview.views import DatatableView
from .models import Post
class MyView(DatatableView):
    model = Post
    datatable_options = {
        'columns': [
            'title',
            'body',
            'created',
            ]
    }

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.MyView.as_view(), name='myview'),
]

post_list.html

{% load static %}
<!-- myapp/mymodel_list.html -->

<!-- Load dependencies -->
<script src="https://code.jquery.com/jquery-3.3.1.min.js"
        integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
        crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>

<!-- Load js for initializing tables via their server-side options -->
<script type="text/javascript" charset="utf8" src="{% static 'js/datatableview.js' %}"></script>
<script type="text/javascript">
    $(function(){
        datatableview.initialize('.datatable');
    });
</script>

<!-- Render the table skeleton, includes the .datatable class for the on-ready initializer. -->
{{ datatable }}

console error:

  query-3.3.1.min.js:2 jQuery.Deferred exception: $$.each is not a 
   function TypeError: $$.each is not a function
        at Object.initialize 
   (http://127.0.0.1:8000/static/js/datatableview.js:20:12)
        at HTMLDocument.<anonymous> (http://127.0.0.1:8000/:17:23)
        at l (https://code.jquery.com/jquery-3.3.1.min.js:2:29375)
        at c (https://code.jquery.com/jquery-3.3.1.min.js:2:29677) 
    undefined
    w.Deferred.exceptionHook @ jquery-3.3.1.min.js:2
    c @ jquery-3.3.1.min.js:2
    setTimeout (async)
    (anonymous) @ jquery-3.3.1.min.js:2
    u @ jquery-3.3.1.min.js:2
    fireWith @ jquery-3.3.1.min.js:2
    fire @ jquery-3.3.1.min.js:2
    u @ jquery-3.3.1.min.js:2
    fireWith @ jquery-3.3.1.min.js:2
    ready @ jquery-3.3.1.min.js:2
    _ @ jquery-3.3.1.min.js:2
    jquery-3.3.1.min.js:2 Uncaught TypeError: $$.each is not a function
        at Object.initialize (datatableview.js:20)
        at HTMLDocument.<anonymous> ((index):17)
        at l (jquery-3.3.1.min.js:2)
        at c (jquery-3.3.1.min.js:2)

Only the table header is being rendered without the data being populated. Any ideas as to what may be going on. As I said most of the answers on here are mentioning that jquery isn't being loaded first but this clearly isn't what is going on in the code above.

解决方案

I had exactly the same issue. So instead of

<script type="text/javascript">
    $(function(){
        datatableview.initialize('.datatable');
    });
</script>

We have to init datatable so:

<script type="text/javascript">
    var opts = {};
    var datatable = datatableview.initialize($('.datatable'), opts);
    var table = datatable.api();
</script>

And in your views.py, instead of

class MyDatatableView(DatatableView):
    model = Revenue
    columns = ["title", "body", "created"]
    search_fields = ["title", "body"]

You have to do (with the model or the query_set in the DatatableView:

class MyDatatable(Datatable):
    class Meta:
        columns = ["title", "body", "created"]
        search_fields = ["title", "body"]


class MyDatatableView(DatatableView):
    model = Revenue
    datatable_class = MyDatatable

But then I get the following js essor, any idea? I'm using jQuery 3.3.1 and this version of datatable: http://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js

Uncaught TypeError: datatable.api is not a function
    at HTMLDocument.<anonymous> (datatable:187)
    at c (jquery.min.js:3)
    at Object.fireWith [as resolveWith] (jquery.min.js:3)
    at Function.ready (jquery.min.js:3)
    at HTMLDocument.H (jquery.min.js:3)

I just found the reason, the api call must be api and not api() because the () are added in the datatableview.js

var table = datatable.api;

My new issue is the search, it returns a 500 server error wichi is "FieldError('Related Field got invalid lookup: {}'.format(lookup_name))" But even if I add the column on which I want to search like "title__name", I still have the alert in the search stating:

DataTables warning: table id=DataTables_Table_0 - Ajax error. For more information about this error, please see http://datatables.net/tn/7

这篇关于数据表未在 django-datatable-view 中填充数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆