13_分页器

分页器

使用方法:

# 导入
from django.core.paginator import Paginator
# 在视图函数中
def test(request):
    book_list = Book.objects.all() # 所有书籍对象
    
    # 实例化出一个对象,第一个参数是要分页的书籍对象,第二个参数是每页多少条数据
    paginator = Paginator(book_list,10)
    
    # -----------------------------对象内的属性----------------------------
    
    # 数据总条数--100条
    print(paginator.count)
    
    # 总页数--10页
    print(paginator.num_pages)
    
    # 页码数的列表
    print(paginator.page_range)
    
    # 取到第 x 页 ,返回一个Page对象
    current_page=paginator.page(5)
    
    # 当前页码内所有的数据
    print(current_page.object_list)
    
    # 是否有下一页
    print(current_page.has_next())
    # 是否有上一页
    print(current_page.has_previous())
    
    # 下一页页码数
    print(current_page.next_page_number())
    # 上一页的页码数
    print(current_page.previous_page_number())

简单小案例:

视图函数页面
# views.py
def test(request):
    book_list = models.Book.objects.all()
    paginator = Paginator(book_list, 5)

    try:
        # 通过GET请求方式取出当前页码数
        current_page_num = int(request.GET.get('page'))

        # 通过当前页码数,返回一个page对象
        current_page = paginator.page(current_page_num)
        
        if paginator.num_pages > 11:
            if current_page_num - 5 < 1:
                page_range = range(1, 12)
            elif current_page_num + 5 > paginator.num_pages:
                page_range = range(paginator.num_pages - 10, paginator.num_pages + 1)
            else:
                page_range = range(current_page_num - 5, current_page_num + 6)
        else:
            page_range = paginator.page_range
    except Exception as e:
        current_page_num = 1
    return render(request, 'test.html', locals())
html页面




    
    
    
    图书


{% for book in current_page %} {% endfor %}
书籍名 书籍价格
{{ book.name }} {{ book.price }}

HTML页面讲解:


        {% for book in current_page %}
        
        {% endfor %}
    
书籍名 书籍价格
{{ book.name }} {{ book.price }}
上下页:


{% if current_page.has_previous %}
  • {% else %}
  • {% endif %}
    
    {% for foo in page_range %}
        
        {% if current_page_num == foo %}
            
  • {{ foo }}
  • {% else %}
  • {{ foo }}
  • {% endif %} {% endfor %}

    ==================================================================

    分页器模板
    
    
    后台代码
     # 分页器
            paginator = Paginator(article_list, 3)
            try:
                current_page_num = int(request.GET.get('page'))
                current_page = paginator.page(current_page_num)
            except Exception as e:
                current_page_num = 1
                current_page = paginator.page(current_page_num)
            if paginator.num_pages > 11:
                if current_page_num - 5 < 1:
                    page_range = range(1, 12)
                elif current_page_num + 5 > paginator.num_pages:
                    page_range = range(paginator.num_pages - 10, paginator.num_pages + 1)
                else:
                    page_range = range(current_page_num - 5, current_page_num + 6)
            else:
                page_range = paginator.page_range
    

    你可能感兴趣的:(13_分页器)