zoukankan      html  css  js  c++  java
  • 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})
  • 相关阅读:
    Chapter 2 JavaScript Basic
    第一感觉:依赖注入读书笔记之一(草稿版)
    JQuery IN ACTION读书笔记之一: JQuery选择器
    使用Razor
    非侵入式Ajax
    QT定制有标题的扁平化下拉框控件
    JavaScript之this,new,delete,call,apply
    实例解析C++虚表
    Python解析生成XML-ElementTree VS minidom
    从Python传递JSON到JavaScript
  • 原文地址:https://www.cnblogs.com/zhangjian0092/p/11006691.html
Copyright © 2011-2022 走看看