简体   繁体   English

我在配置 django-rest-framework-datatables 时遗漏了什么,出现错误“DataTables 警告:表 id=test - Ajax 错误”

[英]Is there anything i missed in configuring django-rest-framework-datatables, getting error “DataTables warning: table id=test - Ajax error”

I tried to configure datatables with rest frame work, i'm getting error when page loads all the datatables fields like pagination, search and title is showing but no data is showing.我尝试使用 rest 框架配置数据表,当页面加载所有数据表字段(如分页、搜索和标题)时出现错误,但没有显示数据。 what might be the reason?可能是什么原因?

serializers.py序列化程序.py

class PermissionSerializer(serializers.ModelSerializer):
class Meta:
    model = Permission
    fields = (
        'name', 'code', 'app',
    )

views.py视图.py

from rest_framework import viewsets
from .serializers import PermissionSerializer

class PermissionViewSet(viewsets.ModelViewSet):
    queryset = Permission.objects.all()
    serializer_class = PermissionSerializer


class ViewallPerms(View):
    def get(self, request):
        context = {
            'a' : 'a',
        }
        return render(request, 'flamika_admin/view_allpermissions.html', context)

urls.py网址.py

 url(r'^perms/$', views.PermissionViewSet, name='perms'),
 path('all-perms', login_required(views.ViewallPerms.as_view(), login_url='f_admin:admin_login'), name='all-perm'),

view_allpermissions.html view_allpermissions.html

<script src="https://code.jquery.com/jquery-1.8.0.min.js"></script>

<div class="row">
    <div class="col-sm-12 col-xs-12">
        <table id="test" class="table table-striped table-bordered" style="width:100%">
        <thead>
            <tr>
                <th>Code</th>
                <th>Name</th>
                <th>App</th>
            </tr>
        </thead>

    </table>
</div>
</div>
<script>
    $(document).ready(function() {
        var table = $('#test').DataTable({
            "serverSide": true,
            dataSrc: "",
            "ajax": "{% url 'flamika_admin:perms' %}",
            "columns": [
                {"data": "name"},
                // Use dot notation to reference nested serializers.
                // This data: could alternatively be displayed with the serializer's ReadOnlyField as well, as seen in the minimal example.
                {"data": "code"},
                {"data": "app"},
           ]
        });
        $('.btn-decade').on('click', function() {
            table.columns().search('');
            var rel = $(this).attr('rel');
            if (rel) {
                table.columns(3).search('^' + rel + '[0-9]$', true).draw();
            } else {
                table.draw();
            }
        });
        $('#albums_minimal').DataTable({
           "search": {"regex": true},
           "language": {"searchPlaceholder": "regular expression"}
        });
    });
    </script>

please let me know where i went wrong, is this right way to configure server-side with datatables.请让我知道我哪里出错了,这是用数据表配置服务器端的正确方法吗? Please correct me.请纠正我。 I wanted to display all data in that model.我想显示 model 中的所有数据。

You forgot to add "?format=datatables" in your ajax API call.您忘记在 ajax API 调用中添加"?format=datatables" Datatables expect response in a certain way to display it properly and if you have correctly configured django-rest-framework-datatables (follow the docs, its pretty much straightforward), the library checks if the format parameter is set to datatables or not. Datatables 期望以某种方式响应以正确显示它,如果您正确配置django-rest-framework-datatables (按照文档,它非常简单),库会检查format参数是否设置为 datatables。 In case its not set, the default pagination format of DRF is used(which does not work with datatables, your case) where if set then django-rest-framework-datatables formats the response in the correct way for datatables.如果未设置,则使用 DRF 的默认分页格式(不适用于数据表,您的情况),如果设置,则django-rest-framework-datatables以正确的方式为数据表格式化响应。

Note: So i wanted to mention if you would like to use django's reverse feature even in js file there is a good library i will link https://github.com/ierror/django-js-reverse .注意:所以我想提一下,如果你想使用 django 的反向功能,即使在 js 文件中有一个很好的库,我将链接https://github.com/ierror/django-js-reverse

Use this to reverse and add ?format=datatables to the url or you could write the url manually.使用它来反转并将?format=datatables添加到 url 或者您可以手动编写 url。 Eg例如

"ajax": {
        "url": "http://127.0.0.1:8000/perms/?format=datatables",
        "type": "GET",
        "headers": {
                /* any headers */
       },
       "dataSrc": "Mention your data source from the respone "
}

Check the url there, make sure its right.检查 url 那里,确保其正确。 I have just written that as an example.我刚刚写了一个例子。

Update: The url routing for PermissionViewSet was incorrect.更新: PermissionViewSet的 url 路由不正确。 Correct form is:正确的形式是:

url(r'^perms/$', PermissionViewSet.as_view({"get": "list"}), name="perms")

Notice the mapping of request method.注意请求方法的映射。

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

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