安装
django-datatables-view
pip install django-datatables-view
前端配置-JS部分
$('#mytable').DataTable({
"paging": true,
"lengthChange": true,
"searching": true,
"ordering": true,
"info": true,
"autoWidth": true,
"lengthMenu": [[20,50,100, -1], [20,50,100, "All"]],
"sPaginationType": "full_numbers",
"bProcessing": true, //开启读取服务器数据时显示正在加载中……特别是大数据量的时候,开启此功能比较好
"bServerSide": true, //开启服务器模式
"sAjaxSource": "{% url 'proxy_list_json' %}", //给服务器发请求的url
});
django 后台响应部分
from django_datatables_view.base_datatable_view import BaseDatatableView
def proxyAdmin(req):
#rows=Proxy.objects.all()[:100]
return render_to_response('data/proxyadmin.html')
class ProxyListJson(BaseDatatableView):
# The model we're going to show
model = Proxy #要分页的类
# define the columns that will be returned
columns = ['ip', 'description', 'score', 'logdate'] #需要显示的字段
# define column names that will be used in sorting
# order is important and should be same as order of columns
# displayed by datatables. For non sortable columns use empty
# value like ''
order_columns = ['ip','description', 'score', 'logdate'] #排序
# set max limit of records returned, this is used to protect our site if someone tries to attack our site
# and make it return huge amount of data
max_display_length = 500
def render_column(self, row, column):
return super(ProxyListJson, self).render_column(row, column)
def filter_queryset(self, qs):
# use parameters passed in GET request to filter queryset
qs_params = None
search = self.request.GET.get(u'sSearch', None)
if search: #模糊搜索
q = Q(ip__contains=search)|Q(description__contains=search)
qs_params = qs_params | q if qs_params else q
qs = qs.filter(qs_params)
return qs
URLS配置
url(r'^paging_proxy/$', ProxyListJson.as_view(), name='proxy_list_json'),
Datables 服务端分页for DJango
原创文章标签 Datables 服务端分页for DJ 文章分类 后端开发
©著作权归作者所有:来自51CTO博客作者skskevin的原创作品,请联系作者获取转载授权,否则将追究法律责任
![](https://ucenter.51cto.com/images/noavatar_middle.gif)
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
datatables服务器分页
问题最近使用了bootstrap的datat...
分页 数据 搜索 数据模型 ip地址