数据表未使用Djangodatatablevi显示

2024-10-03 15:31:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力实现数据表.js使用django datatable视图(不要与django datatables视图混淆),并从零配置数据表类型开始:

http://django-datatable-view.appspot.com/zero-configuration/

我已经将模板和视图类实现复制到我的应用程序和页面显示中,但没有内容。标题存在,但内容完全不存在。如果我将{{object_list}}添加到模板中,那么将显示一个被截断的queryset对象列表,但不会呈现该表。在

这是我的基准.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> 

以下是模板:

^{pr2}$

下面是视图:

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)...']>

因为我基本上是直接从包的示例应用程序中复制的,所以我希望它以类似的方式呈现表。不过,很明显,我出了点问题。在


Tags: textcoreobjecttypejslinkscriptstatic
1条回答
网友
1楼 · 发布于 2024-10-03 15:31:15

所以在经历了很多挫折之后。我发现问题是由于基本.html文件。我希望这能帮助其他人解决这个问题。在

基本上,我收到了两个错误,这两个错误阻止了表的正确显示。在

第一个错误:Error("Bootstrap's JavaScript requires jQuery")

这是因为引导的jQuery导入早于基本jQuery导入。在

第二个错误:Deferred exception: datatable.DataTable is not a function TypeError

这也是由于数据表.jsjQuery导入在基jQuery导入之前。在

简单的细节,但对未来很好的了解。在

相关问题 更多 >