from django.views.generic.list import BaseListView from blog.models import Blog from django.http import HttpResponse class BlogListView(BaseListView): model = Blog # 每页条数 paginate_by = 20 def get_paginate_by(self, queryset): return self.request.GET.get('page_size') or self.paginate_by def render_to_response(self, context): paginator = context['paginator'] # 分页 current_page = context['page_obj'] # 页号 blogs = current_page.object_list data = { 'blog_list': [ { 'id': blog.id, 'title': blog.title, } for blog in blogs ], 'paginator': { 'total_count': paginator.count, 'num_pages': paginator.num_pages, 'page_size': paginator.per_page, 'page_number': current_page.number, } } return HttpResponse(data.values())