我尝试使用休息框架配置数据表,当页面加载所有数据表字段(如分页、搜索和标题)时出现错误,但没有显示数据。可能是什么原因?
序列化程序.py
class PermissionSerializer(serializers.ModelSerializer):
class Meta:
model = Permission
fields = (
'name', 'code', 'app',
)
视图.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)
网址.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
<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>
请让我知道我哪里出错了,这是用数据表配置服务器端的正确方法吗?请纠正我。我想显示该模型中的所有数据。