安装
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‘),
本文出自 “水草” 博客,请务必保留此出处http://961911.blog.51cto.com/951911/1713386
原文地址:http://961911.blog.51cto.com/951911/1713386