【发布时间】:2019-07-11 09:58:06
【问题描述】:
我已经开始了一个新的 Django 项目,试图测试 django-datatable-view。
我收到一个 JS 错误,说 Uncaught TypeError: $$.each is not a function。虽然遵循库网站上的代码 jQuery 是在 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 }}
控制台错误:
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)
只呈现表头而不填充数据。关于可能发生的事情的任何想法。正如我所说,这里的大多数答案都提到没有首先加载 jquery,但这显然不是上面代码中发生的事情。
【问题讨论】:
-
你也可以在这里添加你的 urls.py。您在上面添加的视图仅返回 JSON 数据
-
确定我已经在上面添加了。那么你会如何建议呢?我正在尝试按照图书馆 github 页面上的指南进行操作。它没有提及应如何返回数据。感谢您的帮助
-
我在开始使用这个库时遇到了类似的问题。看起来您还没有创建将处理和提供数据的数据表类。我发现下面 github 页面上的演示非常有帮助。尝试在此 github 下载现场演示,以便您可以查看代码并对其进行调整,直到它中断而不是相反。 github.com/pivotal-energy-solutions/django-datatable-view
标签: jquery django datatables django-datatable