【原】django实现列表分页功能
在view.py里添加分页查询方法:
1 from django.http import JsonResponse 2 from django.views.decorators.http import require_http_methods 3 from django.core import serializers 4 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger 5 import json 6 7 # 分页查询 8 def show_page(request): 9 page = request.GET.get('page') 10 pageSize = int(request.GET.get('pageSize')) 11 response = {} 12 book_list = Book.objects.all() 13 paginator = Paginator(book_list, pageSize) 14 response['total'] = paginator.count 15 try: 16 books = paginator.page(page) 17 except PageNotAnInteger: 18 books = paginator.page(1) 19 except EmptyPage: 20 books = paginator.page(paginator.num_pages) 21 response['list'] = json.loads(serializers.serialize("json", books)) 22 return JsonResponse(response)