简体   繁体   English

Django - 其余框架的数据表ajax错误

[英]Django - Datatables ajax error with rest framework

I'm trying to use rest in conjunction with Datatables to display a large list with server-side processing.我正在尝试将 rest 与Datatables结合使用来显示带有服务器端处理的大列表。 On the datatable html template i get an empty table with an error在数据表 html 模板上,我得到一个有错误的空表

"DataTables warning: table id=bib - Ajax error. For more information about this error, please see http://datatables.net/tn/7" . “DataTables 警告:table id=bib - Ajax 错误。有关此错误的更多信息,请参阅http://datatables.net/tn/7”

urls.py网址.py

from . import views as v
from rest_framework import routers

router = routers.DefaultRouter()
router.register(r'Bibrest51', v.BibViewSet)

urlpatterns = [
    path('home/', include(router.urls)),
    
    path('home/', v.home, name='home'),
    path('', v.home, name='home'),

views.py视图.py

class Get_bib(viewsets.ModelViewSet):
    queryset = Bibrest51.objects.all()
    serializer_class = BibSerializer

home.html主页.html

<table id="bib" class="table table-striped table-bordered" style="width:100%" data-server-side="true" 
        data-ajax="/home/Bibrest51/?format=datatables">
   <thead>
      <tr>
         <th data-data="autor" class ="text-center all">Autor</th>
         <th data-data="ano" class ="text-center all">Ano</th>
         <th>Título</th>
         <th data-data="tipo" class ="text-center not-mobile">Tipo</th>
         <th data-data="tema" class ="text-center not-mobile">Tema</th>
      </tr>
   </thead>
   <tbody>
 
   </tbody>
</table>

JS: JS:

 <script>
        $(document).ready(function() {
          $('#bib').DataTable();
      });
  
  </script>

models.py模型.py

class Bibrest51(models.Model):
    cadastro_id = models.AutoField(primary_key=True)
    autor = models.CharField(db_column='Autor', max_length=255)
    ano = models.CharField(db_column='Ano', max_length=255)
    titulo = models.CharField(db_column='Titulo', max_length=255)
    referencia = models.CharField(db_column='Referencia', max_length=255)


serializers.py序列化程序.py

from rest_framework import serializers
from .models import Bibrest51

class BibSerializer(serializers.ModelSerializer):
    class Meta:
        model = Bibrest51
        fields = ['autor', 'ano','tipo','tema']

urls.py网址.py

path('get_bib/',Get_bib.as_view({'get': 'list'}), name="get-bib"),

JS: JS:

<script>
    $(document).ready(function() {
     var table = $('#bib').DataTable({
     "responsive": true,
     "destroy": true,
     "serverSide": true,
     "ajax": "/get_bib?format=datatables",

  "columns": [
    { "data": "autor", "sortable": false },
    { "data": "ano", "sortable": false },
    { "data": "tipo", "sortable": false },
    { "data": "tema", "sortable": false },
  ],
  'order': [[1, 'asc']],
  'pageLength': 5,
  'lengthMenu': [[5, 10, 20,], [5, 10, 20,]],
  "oLanguage": {
    "sLengthMenu": "Show _MENU_"
  }
});
  });

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

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