添加行会中断Django中的数据表

添加行会中断Django中的数据表,django,datatables,django-templates,Django,Datatables,Django Templates,我在Django中有一个datatable,它在硬编码时可以正常工作,但当我添加Django模板标记时,它就会中断。页面上的检查显示:uncaughttypeerror:无法在jquery.datatables.min.js中设置未定义的的属性'\u DT\u CellIndex' 只有当表中有多个用户,或者尝试使用django在表中添加列时,才会发生这种情况。因为我将在我的项目中使用多个数据表,所以我需要找出我做错了什么。我使用的DataTableJS代码来自一个模板,我不确定错误是在模板代码

我在Django中有一个datatable,它在硬编码时可以正常工作,但当我添加Django模板标记时,它就会中断。页面上的检查显示:
uncaughttypeerror:无法在jquery.datatables.min.js中设置未定义的
的属性'\u DT\u CellIndex'

只有当表中有多个用户,或者尝试使用django在表中添加列时,才会发生这种情况。因为我将在我的项目中使用多个数据表,所以我需要找出我做错了什么。我使用的DataTableJS代码来自一个模板,我不确定错误是在模板代码中还是在Django模板代码中。所以请原谅长代码块

employees.html(django模板):


Jquery.datatables.min.js和datatables.bootstrap4.min.js也被使用,但它们都来自bootstrap4。除非需要,否则我不会将它们添加到这里,而且它们已经缩小了。

只有当表或标记的数据不可用时,才会出现此问题

您在使用django templatestag的模板中有条件,因此表中属于
元素的子元素的每个
元素的数量与属于该元素的子元素的
元素的数量不匹配。 请确保您在
标签中有相同数量的
标签

编辑:


可能是{%if not profile.user.is_superuser%}这个条件造成了这个问题。如果用户是超级用户,则不会创建与

中相同数量的
不匹配的
标记。我理解这一点,但每个用户的数据都是相同的。那么为什么会有问题呢?如果用户是超级用户,那么{%If not profile.user.is_superuser%}这个条件可能会产生问题。如果我只有一个额外的用户,超级用户代码的数量将不起作用。当我向用户添加第二个非超级用户时,表中断扫描您添加有问题的html输出代码,这将有助于理解表的结构?删除了超级用户检查,它就工作了。但我不明白为什么它只适用于一个非超级用户,而不适用于一个以上的用户。但是你的回答是正确的,我做了标记。谢谢真的很感激!
<div class="card-body collapse in">
                    <div class="card-block card-dashboard">
                        <button id="addRow" class="btn btn-primary mb-2 js-create-employee"><i class="ft-plus"></i>&nbsp; Add New Employee</button>
                        <table class="table table-striped table-bordered zero-configuration">
                            <thead>
                                <tr>
                                    <th>Name</th>
                                    <th>Username</th>
                                    <th>Roles</th>
                                    <th>Email</th>
                                    <th>Mobile Contact</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody>

                                {% for profile in user_profile_list %}
                                    <tr>
                                    {% if not profile.user.is_superuser %}
                                        <td><a href="{% url 'dispatch:profile_detail' pk=profile.user_id %}">{{ profile.user.get_full_name }}</a></td>
                                        <td>{{ profile.user.username }}</td>
                                        <td>
                                            {% for g in profile.user.groups.all %}
                                               <div class="tag tag-default">{{ g.name|split:'_'|title }}</div>
                                            {% endfor %}
                                        </td>
                                        <td>{{ profile.user.email }}</td>
                                        <td>{{ profile.mobile_phone }}</td>
                                        <td><a href="#" alt="Edit"><i class="fa fa-pencil"></i></a>&nbsp;&nbsp;<a href="#" alt="Assign"><i class="fa fa-link"></i><a/>&nbsp;&nbsp;<a href="#" alt="delete"><i class="fa fa-times"></i><a/></td>

                                        {% endif %}
                                    </tr>
                                    {% endfor %}
                            </tbody>
                            <tfoot>
                                <tr>
                                    <th>Name</th>
                                    <th>Username</th>
                                    <th>Roles</th>
                                    <th>Email</th>
                                    <th>Mobile Contact</th>
                                    <th>Actions</th>
                                </tr>
                            </tfoot>
                        </table>
$(document).ready(function() {

/****************************************
*       js of zero configuration        *
****************************************/

$('.zero-configuration').DataTable();
});