安装
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'),
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。