paginator 使用
def get(self, request, *args, **kwargs):
rs_data = {
'count': 0,
'items': []
}
page = int(self.request.GET.get('page', 1))
page_size = int(self.request.GET.get('page_size', 10))
name = self.request.GET.get('name')
obj_list = Slider.objects.order_by('sort', '-add_datetime')
if name:
obj_list = obj_list.filter(name__icontains=name)
if obj_list:
rs_data['count'] = obj_list.count()
pnr = Paginator(obj_list, page_size)
try:
item_obj = pnr.page(page)
except EmptyPage:
item_obj = pnr.page(pnr.num_pages)
for item in item_obj:
tmp_data = {
'uuid': item.uuid,
'add_datetime': item.add_datetime,
'name': item.name,
'img': item.img,
'sort': item.sort,
'link': item.link,
}
rs_data['items'].append(tmp_data)
return JsonResponse(rs_data)
django对mysql的分页查询其实就是使用切片实现的
def get(self, request, *args, **kwargs):
rs_data = {
'count': 0,
'items': []
}
page = int(self.request.GET.get('page', 1))
page_size = int(self.request.GET.get('page_size', 10))
name = self.request.GET.get('name')
obj_list = Slider.objects.order_by('sort', '-add_datetime')
if name:
obj_list = obj_list.filter(name__icontains=name)
limit = (page - 1) * page_size
offset = page * page_size
rs_data['count'] = obj_list.count()
obj_list = obj_list[limit: offset]
if obj_list:
for item in obj_list:
tmp_data = {
'uuid': item.uuid,
'add_datetime': item.add_datetime,
'name': item.name,
'img': item.img,
'sort': item.sort,
'link': item.link,
}
rs_data['items'].append(tmp_data)
return JsonResponse(rs_data)
标签:paginator,obj,name,list,django,item,数据量,data,page
From: https://www.cnblogs.com/guanchaoguo/p/17814467.html