• 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})
  • 相关阅读:
    【转】【VS2008无法启动asp.net development server】的解决
    C#运用技巧(1)
    C# — WinForm 基本控件
    TB 需求分析
    C# 远程连接SQL 2005数据库
    SQL语句的运用
    如何跌倒
    国学堂-梁冬对话张长琳《人体的彩虹》系列
    帝范:中国最伟大帝王的沉思录
    web.xml 配置
  • 原文地址:https://www.cnblogs.com/zhangjian0092/p/11006691.html
Copyright © 2020-2023  润新知