[英]Datatable not displaying using Django-datatable-view
我正在嘗試使用 django-datatable-view 實現 datatables.js,(不要與 django-datatables-view 混淆),並且已經從零配置數據表類型開始:
http://django-datatable-view.appspot.com/zero-configuration/
我已將模板和視圖類實現復制到我的應用程序和頁面顯示中,但沒有內容。 標題存在,但內容完全不存在。 如果我將 {{ object_list }} 添加到模板中,則會顯示查詢集對象的截斷列表,但不會呈現表。
這是我的 base.py
{% load static i18n %}
<html>
<head>
{% block static %}
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
<title>{% block page_title %}{% endblock %}</title>
<meta name="description" content="{% block description %}{% endblock %}">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="application-name" content="core-Net.uk" />
{# Bootstrap #}
<!-- Bootstrap Core CSS -->
<link href="{% static 'core/site/vendor/bootstrap/css/bootstrap.min.css' %}" rel="stylesheet">
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<!-- toastr -->
<link href="{% static 'core/toastr/toastr.min.css' %}" rel="stylesheet" >
<!-- Custom Fonts -->
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp" crossorigin="anonymous">
<!-- Star rating -->
<link rel="stylesheet" href="{% static 'star-ratings/css/star-ratings.css' %}">
<!-- Custom CSS -->
<link href="{% static 'core/site/css/site.css' %}" rel="stylesheet" >
{# jQuery #}
<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
{# datatables.js #}
<script type="text/javascript" charset="utf8" src="http://cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://cdn.datatables.net/1.10.9/css/jquery.dataTables.css">
<link rel="stylesheet" type="text/css" href="{% static 'datatables/datatables.min.css' %}"/>
<script type="text/javascript" src="{% static 'datatables/datatables.min.js' %}></script>
{# django-datatable-view #}
<script type="text/javascript" charset="utf8" src="{% static 'core/js/datatableview.js' %}"></script>
<script type="text/javascript" charset="utf8" src="{% static 'core/js/datatableview.min.js' %}"></script>
{# code highlighting #}
<link href="{% static 'core/syntaxhighlighter/shCore.css' %}" rel="stylesheet" type="text/css" />
<link href="{% static 'core/syntaxhighlighter/shThemeDefault.css' %}" rel="stylesheet" type="text/css" />
<script src="{% static 'core/syntaxhighlighter/shCore.js' %}" type="text/javascript"></script>
<script src="{% static 'core/syntaxhighlighter/shAutoloader.js' %}" type="text/javascript"></script>
<script src="{% static 'core/syntaxhighlighter/shBrushPython.js' %}" type="text/javascript"></script>
<script type="text/javascript" charset="utf8">
datatableview.auto_initialize = true;
$(function(){
datatableview.initialize('.datatable');
});
</script>
</script>
{# test_project helpers #}
<style type="text/css">
.nav.sidebar a {
border-right: 1px solid white;
padding: 5px 10px;
}
.nav.sidebar a.active {
font-weight: bold;
background-color: #f0f0f0;
border-color: #428bca;
}
.nav.sidebar li.divider {
margin: 0.5em;
border-top: 1px solid #dfdfdf;
}
</style>
<script type="text/javascript">
$(function(){
SyntaxHighlighter.all();
var path = window.location.pathname;
$('.nav.sidebar > li > a[href="'+path+'"]').addClass("active");
});
</script>
{% endblock static %}
<!-- Favicon -->
{% block css %}{% endblock %}
{% block extra_head %}{% endblock %}
<!-- Cookie Consent -->
<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.3/cookieconsent.min.css" />
<script src="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.3/cookieconsent.min.js"></script>
<script>
window.addEventListener("load", function(){
window.cookieconsent.initialise({
"palette": {
"popup": {
"background": "#edeff5",
"text": "#838391"
},
"button": {
"background": "#4b81e8"
}
},
"theme": "edgeless",
"position": "bottom-right",
"content": {
"href": "/legal/privacy-policy/"
}
})});
</script>
</head>
這是模板:
{% extends 'base.html' %}
{% load static i18n %}
<script type="text/javascript" charset="utf8">
datatableview.auto_initialize = true;
$(function(){
datatableview.initialize('.datatable');
});
</script>
{% block content %}
{{ datatable }}
{{ object_list }}
{% endblock content %}
這是視圖:
class ZeroConfigurationDatatableView(DatatableView):
model = SSTnp
template_name = 'core/sstnp_list.html'
class datatable_class(Datatable):
class Meta:
model = SSTnp
structure_template = 'datatableview/default_structure.html'
結果:
ID Name Location
<QuerySet [<SSTnp: SSTnp object (ss )>, <SSTnp: SSTnp object (ss )>, <SSTnp: SSTnp object (ss )>, <SSTnp: SSTnp object (ss )>, <SSTnp: SSTnp object (ss )>, <SSTnp: SSTnp object (ss )>, <SSTnp: SSTnp object (ss )>, <SSTnp: SSTnp object (ss )>, <SSTnp: SSTnp object (ss )>, <SSTnp: SSTnp object (ss )>, <SSTnp: SSTnp object (ss )>, '...(remaining elements truncated)...']>
因為我基本上只是直接從包的示例應用程序中復制了它,所以我希望它能夠以類似的方式呈現表格。 然而,很明顯,我出了點問題。
所以在經歷了很多挫折之后。 我發現問題是由於我的 base.html 文件中的導入順序造成的。 我希望這可以幫助其他人解決這個問題。
本質上,我收到了兩個錯誤,這些錯誤阻止了我的表格的正確顯示。
第一個錯誤: Error("Bootstrap's JavaScript requires jQuery")
這是因為 Bootstrap 的 jQuery 導入先於基礎 jQuery 導入。
第二個錯誤: Deferred exception: datatable.DataTable is not a function TypeError
這同樣是因為 DataTable.js jQuery 導入在基本 jQuery 導入之前。
簡單的細節,但很高興知道未來。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.