You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
976 B
37 lines
976 B
2 years ago
|
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())
|
||
|
|
||
|
|
||
|
|