简体   繁体   English

数据表未在django-datatable-view中填充数据

[英]Data table not populating data in django-datatable-view

I've started a new Django project with trying to test out django-datatable-view . 我已经开始尝试尝试django-datatable-view来启动一个新的Django项目。

I'm getting a JS error saying Uncaught TypeError: $$.each is not a function. 我收到一个JS错误,提示未捕获的TypeError:$$。each不是一个函数。 Although following the code on the library's website jQuery is being loaded before datatableview.js. 尽管遵循图书馆网站上的代码,但jQuery在datatableview.js之前加载。

models.py 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 views.py

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

urls.py urls.py

from django.urls import path
from . import views

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

post_list.html 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. 正如我所说的,这里的大多数答案都提到并没有首先加载jquery,但这显然不是上面代码中的问题。

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 在您的views.py中,

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: 您必须要做的(使用DatatableView中的模型或query_set:

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? 但是随后我得到了以下js essor,知道吗? I'm using jQuery 3.3.1 and this version of datatable: http://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js 我正在使用jQuery 3.3.1和此版本的数据表: 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 我刚刚找到原因,api调用必须是api而不是api(),因为()已添加到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: 我的新问题是搜索,它返回500服务器错误,错误是“ FieldError('Related Field get invalid lookup:{}'。format(lookup_name))”。但是,即使我添加了要搜索的列,例如“ title__name”,搜索中仍然显示警告:

DataTables warning: table id=DataTables_Table_0 - Ajax error. DataTables警告:表格ID = DataTables_Table_0-Ajax错误。 For more information about this error, please see http://datatables.net/tn/7 有关此错误的更多信息,请参见http://datatables.net/tn/7

I think the code belongs to 0.8 version and you've updated your package to 0.9. 我认为代码属于0.8版本,并且您已将软件包更新为0.9。 If that's the case you can look at the migration guide here 如果是这种情况,您可以在此处查看迁移指南

the 0.9 version has multiple breaking changes. 0.9版具有多个重大更改。 It no longer supports datatable_options in the class instead, things are shifted to Meta class 它不再在类中支持datatable_options ,而是将其转移到Meta

In addition to openHBP answer, I had to put the initilization in a document.ready : 除了openHBP答案外,我还必须将初始化内容放在document.ready中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM