【发布时间】:2019-07-11 08:39:07
【问题描述】:
我正在使用 django-rest-framework-datatables 并按照说明进行操作,但无法开始工作。
我遵循了这个:
https://django-rest-framework-datatables.readthedocs.io/en/latest/index.html
我收到警告“DataTables 警告:表 id=transactions - 无效 JSON 响应。有关此错误的更多信息,请参阅 http://datatables.net/tn/1”
我的网址在一个名为“自定义”的应用中
app_name = 'custom'
router = routers.DefaultRouter()
router.register(r'exber', views.ExberViewSet)
urlpatterns = [
url('^exber/api/', include(router.urls)),
url('^exber/', views.exber_index, name='exber'),
]
我的观点:
class ExberViewSet(viewsets.ModelViewSet):
queryset = Exber_transactions.objects.all()
serializer_class = ExberSerializer
def exber_index(request):
return render(request, 'custom/exber/transactions.html)
型号:
class Exber_transactions ( models.Model ):
date = models.DateField ( null = True, blank = True )
desc = models.CharField ( max_length = 100 )
activity = models.CharField ( max_length = 100 )
qty = models.DecimalField ( max_digits = 12, decimal_places = 3, default = 0 )
price = models.DecimalField ( max_digits = 12, decimal_places = 2, default = 0 )
accrued_int = models.DecimalField ( max_digits = 12, decimal_places = 3, default = 0 )
amount = models.DecimalField ( max_digits = 12, decimal_places = 2, default = 0 )
序列化器:
class ExberSerializer(serializers.ModelSerializer):
id = serializers.IntegerField ( read_only = True )
class Meta:
model = Exber_transactions
fields = ('date','desc', 'activity','qty', 'price', 'accrued_int', 'amount')
datatables_always_serialize = ('id',)
这是桌子:
<table id="transactions" class="table table-striped table-bordered" style="width:100%" data-server-side="true" data-ajax="custom/api/exber?format=datatables">
<thead>
<tr>
<th data-data="date">date</th>
<th data-data="desc">desc</th>
<th data-data="activity">activity</th>
<th data-data="qty">qty</th>
<th data-data="price">price</th>
<th data-data="accrued_int">accrued_int</th>
<th data-data="amount">amount</th>
</tr>
</thead>
</table>
<script>
$(document).ready(function() {
$('#transactions').DataTable();
});
</script>
我怀疑它在某处的路径中?我尝试了很多组合。
谢谢。
【问题讨论】:
标签: django datatables