django第11天(分页器)

django第11天分页器

分页模块

批量插入数据
book_list = []
#先生成对象
for i in range(100):
    book = Book(name = 'book%s'%i,price=10+i)
    book_list.append(book)
#操作对象列表,设置一次操作多少条数据
Book.objects.bulk_create(book_list,10)

分页器及其方法

1.导入分页器模块
from django.core.paginator import Paginator

2.生成分页器对象
第一个参数为分页对象列表,第二个参数为一页的数量
paginator = Paginator(book_list,10)

3.分页常用方法
#分页对象的总数
count = paginator.count()
#分成的总页数
num_pages = paginator.num_pages()
#页数的范围
page_range = paginator.page_range

#当前页面数从前台获取
try:
	current_page_num = int(request.GET.get('page',1))
except:
    current_page_num = 1
    
#通过当前页数,获得当前页的对象列表(返回给前台迭代显示对象)    
current_page = paginator.page(current_page_num)

# 是否有上一页
current_page.has_previous()
# 上一页编码
current_page.previous_page_number()
# 是否有下一页
current_page.has_next()
# 下一页编码
current_page.next_page_number()

基础分页器编写

1.后台

from django.core.paginator import Paginator

def index(request):
    #获取图书对象
    book_list = Book.objects.all()
    #生成分页器对象
    paginator = Paginator(book_list,10)
    #获取页码范围,以便前台产生分页个数
    page_range = paginator.page_range
    #从前台获取当前页数,如果是第一次访问就返回第一页
    try:
        current_page_num = int(request.GET.get('page',1))
    except:
        current_page_num =1
    #通过当前页数,获得current_page,可迭代获得当前页所有对象
    current_page = paginator.page(current_page_num)
    return render(request,'index.html',locals())


前台
<div class="container-fluid">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <h1>图书详情</h1>
            <table class="table">
                <tr>
                    <th>图书名称</th>
                    <th>图书价格</th>
                </tr>
                #仅展示当前页数的所有对象
                {% for book in current_page %}
                    <tr>
                        <td>{{ book.name }}</td>
                        <td>{{ book.price }}</td>
                    </tr>
                {% endfor %}
            </table>
            <nav aria-label="Page navigation">
                <ul class="pagination">
                    <li>
                    	#如果存在上一页,当前页码减去1
                        {% if current_page.has_previous %}
                            <a href="?page={{ current_page_num|add:-1 }}" aria-label="Previous">
                        {% else %}
                        #如果不存在不进行操作
                            <a href="javascript:void(0)" aria-label="Previous">
                        {% endif %}
                        <span aria-hidden="true">上一页</span>
                        </a>
                    </li>
                    #通过页数生成分页项
                    {% for foo in page_range %}
                        <li>
                            <a href="?page={{ foo }}">{{ foo }}</a></li>
                        <li>
                    {% endfor %}
                    <li>
                        {% if current_page.has_next %}
                            <a href="?page={{ current_page_num|add:1 }}" aria-label="Next">
                        {% else %}
                            <a href="javascript:void(0)" aria-label="Next">
                        {% endif %}
                        <span aria-hidden="true">下一页</span>
                        </a>
                    </li>
                </ul>
            </nav>
        </div>
    </div>
</div>

高级分页器编写

当对象个数过多,分的页数过多时,需要将分页器优化,不然会显示一大堆的分页

设计要求:
    除了首末页,只显示选中页数,前5页和后5页
前台:
    <div class="container-fluid">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <h1>图书详情</h1>
            <table class="table">
                <tr>
                    <th>图书名称</th>
                    <th>图书价格</th>
                </tr>
                {% for book in current_page %}
                    <tr>
                        <td>{{ book.name }}</td>
                        <td>{{ book.price }}</td>
                    </tr>
                {% endfor %}
            </table>
            <nav aria-label="Page navigation">
                <ul class="pagination">
                    <li>
                        {% if current_page.has_previous %}
                            <a href="?page={{ current_page_num|add:-1 }}" aria-label="Previous">
                        {% else %}
                            <a href="javascript:void(0)" aria-label="Previous">
                        {% endif %}
                        <span aria-hidden="true">上一页</span>
                        </a>
                    </li>
                    {% if current_page_num == 1 %}
                        <li class="active">
                            {% else %}
                        <li>
                    {% endif %}
                    <a href="?page=1">1</a></li>
                    {% if num_pages > 14 %}
                        <li><a href="javascript:void(0)">...</a></li>
                    {% endif %}
                    {% for num in page_range %}
                        {% if current_page_num == num %}
                            <li class="active">
                                {% else %}
                            <li>
                        {% endif %}
                    <a href="?page={{ num }}">{{ num }}</a></li>
                    {% endfor %}
                    {% if num_pages > 13 %}
                        <li><a href="javascript:void(0)">...</a></li>
                    {% endif %}


                    {% if current_page_num == num_pages %}
                        <li class="active">
                            {% else %}
                        <li>
                    {% endif %}
                    <a href="?page={{ num_pages }}">{{ num_pages }}</a></li>
                    <li>
                        {% if current_page.has_next %}
                            <a href="?page={{ current_page_num|add:1 }}" aria-label="Next">
                        {% else %}
                            <a href="javascript:void(0)" aria-label="Next">
                        {% endif %}
                        <span aria-hidden="true">下一页</span>
                        </a>
                    </li>
                </ul>
            </nav>
        </div>


    </div>
</div>
    
    
    
   
    
    
后台: 
def index(request):
    book_list = Book.objects.all()
    paginator = Paginator(book_list,6)
    count = paginator.count
    num_pages = paginator.num_pages
    page_range = paginator.page_range
    try:
        current_page_num = int(request.GET.get('page',1))
    except:
        current_page_num =1
    current_page = paginator.page(current_page_num)
    if num_pages > 13:
        if current_page_num <7 :
            page_range = range(2,12)
        elif current_page_num +6 > num_pages:
            page_range = range(num_pages-10,num_pages)
        else:
            page_range = range(current_page_num-5,current_page_num+6)
    else:
        page_range = range(2,13)
    return render(request,'index.html',locals())
        
posted @ 2019-03-21 09:41  robertzhou  阅读(205)  评论(0编辑  收藏  举报