首页 > 其他 > 详细

Django分页器的设置

时间:2019-03-11 20:27:12      阅读:320      评论:0      收藏:0      [点我收藏+]

Django分页器的设置

 

有时候在页面中数据有多条时很显然需要进行分页显示,那么在python中django可以这样设置一个分页处理

技术分享图片

怎么样去设置呢?

我们要用到  Django  中的  Paginator  组件

技术分享图片

后台代码及解析

from django.core.paginator import Paginator  # 导入该组件
?
def show_book(request):
    title = 图书详情
    header = 图书管理系统
    book_list = Book.objects.all()
    paginator = Paginator(book_list, 8)   # 将返回前台展示数据进行Paginator操作,一页显示8条记录
   
    count = paginator.count    # 对象总个数    
    print(count)
    
    sum_pages = paginator.num_pages  # 总分页数
    
    page_range = paginator.page_range  # 页码列表(可迭代对象) 也就是可以被循环
?
    # 获取第一页
    # 请求链接错误,默认用第一页 主要是防止get请求时候直接写/show_book/?page= 非法字符
    try:
        current_num = int(request.GET.get(page, 1))  # type: str => int
    except:
        current_num = 1
        
    # 不在页面范围内的安全处理 主要是防止get请求时候直接写/show_book/?page=超过总分页数或者小于0
    if current_num < 1:
        current_num = 1
        return redirect(/show_book/?page=%s % current_num)
    elif current_num > sum_pages:
        current_num = sum_pages
        return redirect(/show_book/?page=%s % current_num)
   
    current_page = paginator.page(current_num)   # 当前的页面
    print(>>>, current_page)
    
    # 通过page_range控制页数版面,我设置的是中间显示3个标签和前后两页,其他的用...标识,以下为控制逻辑
    if current_num < 4:     
        page_range = range(2, 5)
    elif current_num > sum_pages - 3:
        page_range = range(sum_pages - 3, sum_pages)
    else:
        page_range = range(current_num - 1, current_num + 2)
    return render(request, show_book.html, locals())

 

前台代码解析

{% extends base.html %}   # 继承母版
?
?
{% block list %}
    <div class="list-group">
        <a href="{% url ‘show_book‘ %}" class="list-group-item active">
            <h4>图书列表</h4>
        </a>
        <a href="{% url ‘show_author‘ %}" class="list-group-item">
            <h4>作者列表</h4>
        </a>
        <a href="{% url ‘show_publish‘ %}" class="list-group-item">
            <h4>出版社列表</h4>
        </a>
    </div>
{% endblock %}
?
{% block table %}
    <div class="clearfix">
        <a href="{% url ‘add_book‘ %}" class="btn btn-primary pull-right">新增图书</a>
    </div>
    <table class="table" style="margin-top: 10px">
        <tr>
            <th>编号</th>
            <th>书名</th>
            <th>价格</th>
            <th>出版日期</th>
            <th>出版社</th>
            <th>作者</th>
            <th>编辑</th>
            <th>删除</th>
        </tr>
        {% for book in current_page %}    # 这里要设置为当前的页面对象,和后台对应
            <tr>
                <td>{{ forloop.counter }}</td>
                <td>{{ book.name }}</td>
                <td>{{ book.price }}</td>
                <td>{{ book.publish_date|date:Y年m月d日 }}</td>
                <td>{{ book.publish.name }}</td>
                <td>
                    {% for author in book.author.all %}
                        {{ author.name }}
                        {% if not forloop.last %}
                            |
                        {% endif %}
                    {% endfor %}
                </td>
                <td>
                    <a href="{% url ‘update_book‘ %}?id={{ book.id }}">编辑</a>
                </td>
                <td>
                    
                    <a onclick="delete_action({{ book.id }})" class="delete-confirm" href="javascript:void(0)">删除</a>
                </td>
            </tr>
        {% endfor %}
    </table>
    <!-- 分页器 -->   # 用jQuery和bootstrap实现
    <nav aria-label="Page navigation">
        <ul class="pagination">
            {#            如果只有一页则不需要上一页和下一页标识#}
            <!-- 上一页 -->   # 设置上一页的按钮
            <li>
                {% if current_num > 1 %}   # 只有页面大于1才会有效果,否则不能点击
                    <a href="{% url ‘show_book‘ %}?page={{ current_num|add:-1 }}" aria-label="Previous">
                {% else %}
                    <a href="javascript:void(0)" aria-label="Previous">
                {% endif %}
                <span aria-hidden="true">&laquo;</span>
                </a>
            </li>
            <!-- 第一页页码 -->    # 设置第一页显示的按钮
            {% if current_num == 1 %}
                <li class="active">
                    {% else %}
                <li>
            {% endif %}
            <a href="{% url ‘show_book‘ %}?page=1">1</a></li>
            {#             只有一页就不需要...和中间三页了#}
            {% if current_num > 3 %}   # 如果页面小于3页就不需要...进行分隔
                <li><a href="javascript:void(0)">...</a></li>
            {% endif %}
            <!-- 中间三页页码 -->     # 设置中间三页的代码
            {% for num in page_range %}
                {% if current_num == num %}
                    <li class="active">
                        {% else %}
                    <li>
                {% endif %}
            <a href="{% url ‘show_book‘ %}?page={{ num }}">{{ num }}</a></li>
            {% endfor %}
?
            {% if current_num < sum_pages|add:-3 %}  # 如果点击到最后三页就没必要显示...
                <li><a href="javascript:void(0)">...</a></li>
            {% endif %}
            <!-- 最后一页页码 -->     # 设置最后一页的显示按钮
            {% if current_num > 1 %}
                {% if current_num == sum_pages %}
                    <li class="active">
                        {% else %}
                    <li>
                {% endif %}
            <a href="{% url ‘show_book‘ %}?page={{ sum_pages }}">{{ sum_pages }}</a></li>
            {% endif %}
?
            <!-- 下一页 -->     # 设置下一页的按钮
            <li>
                {% if current_num < sum_pages %}
                    <a href="{% url ‘show_book‘ %}?page={{ current_num|add:1 }}" aria-label="Next">
                {% else %}
                    <a href="javascript:void(0)" aria-label="Next">
                {% endif %}
                <span aria-hidden="true">&raquo;</span>
                </a>
            </li>
        </ul>
    </nav>

 

Django分页器的设置

原文:https://www.cnblogs.com/5j421/p/10512821.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!