django 自带分页器

def show(request):

    all_books = models.Book.objects.all()

    page_obj = Paginator(all_books,every_page_count) #5每页显示多少条

    print(page_obj.num_pages) #21
    print(page_obj.page_range) #页码从多少到多少,range(1,22)
    print(page_obj.count) #总共有多少条数据 104
    current_page_num = request.GET.get('page',1)
    # page1 = page_obj.page(1)  #第一页所有的model对象数据
    # for i in page1:
    #     print(i)
    # page2 = page_obj.page(2)
    # print(page2) #<Page 2 of 21>
    # print(page2.has_previous()) #True
    # print(page2.has_next()) #True

    page_range = page_obj.page_range
    page_num_objs = page_obj.page(current_page_num)
    all_numbers = page_obj.num_pages

    # if page_num_objs.has_next():
    # print('下一页页码',page_num_objs.next_page_number())
    # print('上一页页码',page_num_objs.previous_page_number())

    if all_numbers > show_page_range : #如果总页码大于5页,咱们就让他显示5个页码
        current_page_num = int(current_page_num)
        if current_page_num <= 3:
            page_range = range(1,6)
        elif current_page_num + 2 > all_numbers:
            page_range = range(all_numbers-4, all_numbers+1)
        else:
            page_range = range(current_page_num - 2, current_page_num + 3)


    # all_books = models.Book.objects.all().first()
    # all_books.authors.all() #多对多
    # all_books.publish.name #一对多
    return render(request,'show.html',{'page_num_objs':page_num_objs,'page_range':page_range,'current_page_num':current_page_num})

 

posted @ 2019-06-11 22:30  眼镜儿  阅读(287)  评论(0编辑  收藏  举报