其他分享
首页 > 其他分享> > Django 分页器组件

Django 分页器组件

作者:互联网

分页器

from django.core.paginator import Paginator
def books_page(request):
    book_list=models.Books.objects.all()
    paginator=Paginator(book_list,10)
    # Paginator对象的属性
    print(paginator.count) 		# 数据总条数
    print(paginator.num_pages) 	# 总页数
    print(paginator.per_page) 	# 每页显示条数
    print(paginator.page_range) # 总页数的索引范围
    print(paginator.page(1))	# page对象
    # Page对象的属性和方法
    # has_next              是否有下一页
    # next_page_number      下一页页码
    # has_previous          是否有上一页
    # previous_page_number  上一页页码
    # object_list           分页之后的数据列表
    # number                当前页
    page=paginator.page(2)
    print(page.has_next())
    print(page.next_page_number())
    print(page.has_previous())
    print(page.previous_page_number())
    print(page.object_list)
    print(page.number)

    return render(request,'book_page.html',locals())

分页器模板

后端

from django.core.paginator import Paginator
def books_page(request):
    # 在get中获取当前页,没有就用1
    current_num = int(request.GET.get('page_num', 1))
    # 获取需要分页的数据
    book_list = models.Books.objects.all()

    # 生成需要分页的对象(需要分页的数据,每页多少条)
    paginator = Paginator(book_list, 20)
    # 切换到当前页,没有就到第一页
    try:
        page = paginator.page(current_num)
    except Exception as e:
        current_num = 1
        page = paginator.page(current_num)

    # 分页图标数量控制
    if paginator.num_pages > 11:
        if current_num - 5 < 1:
            page_range = range(1, 12)
        elif current_num + 5 > paginator.num_pages:
            page_range = range(paginator.num_pages - 10, paginator.num_pages + 1)
        else:
            page_range = range(current_num - 5, current_num + 6)
    else:
        page_range = paginator.page_range

    return render(request, 'book_page.html', locals())

前端

//分页内容提取
<table class="table table-striped">
    <thead>
        <tr>
            <th>id</th>
            <th>书名</th>
            <th>价格</th>
            <th>出版社</th>
        </tr>
    </thead>
    <tbody>
        {% for book in page.object_list %}
        <tr>
            <td>{{ book.id }}</td>
            <td>{{ book.name }}</td>
            <td>{{ book.price }}</td>
            <td>{{ book.publish }}</td>
        </tr>
        {% endfor %}

    </tbody>
</table>

//分页图标制作
<div class="text-center">
    <nav aria-label="Page navigation">
        <ul class="pagination">
            
            //判断是否有上一页
            {% if page.has_previous %}
            <li>
                <a href="/books_page/?page_num={{ page.previous_page_number }}"
                   aria-label="Previous">
                    <span aria-hidden="true">&laquo;</span>
                </a>
            </li>
            {% else %}
            <li class="disabled">
                <a href="" aria-label="Previous">
                    <span aria-hidden="true">&laquo;</span>
                </a>
            </li>
            {% endif %}

			//制作页码
            {% for foo in page_range %}
            {% if current_num == foo %}
            <li class="active"><a href="/books_page/?page_num={{ foo }}">{{ foo }}</a></li>
            {% else %}
            <li><a href="/books_page/?page_num={{ foo }}">{{ foo }}</a></li>
            {% endif %}

            {% endfor %}

            //判断是否有下一页
            {% if page.has_next %}
            <li>
                <a href="/books_page/?page_num={{ page.next_page_number }}" aria-label="Next">
                    <span aria-hidden="true">&raquo;</span>
                </a>
            </li>
            {% else %}
            <li class="disabled">
                <a href="" aria-label="Next">
                    <span aria-hidden="true">&raquo;</span>
                </a>
            </li>
            {% endif %}

        </ul>
    </nav>
</div>

标签:paginator,分页,print,Django,book,range,num,组件,page
来源: https://www.cnblogs.com/shof/p/14646859.html