关于Django框架中分页器的使用

2020-03-14  本文已影响0人  脏脏的小泥娃

最近搬码的时候需要在Django框架中用到分页功能,于是就去百度了一下。

django框架中实现分页功能可以有很多种方式,其中django框架自带了一个分页器库(paginator),相当于一个半成品框架,可以用来帮开发人员快速的实现一个分页功能。

首先分页的大致原理是
1.先从数据库里查询到大量数据集--对应Models.py文件的处理。
2.通过分页器对这些数据进行处理实现分页展示--对应Views.py文件的方法。
3.将数据传到前台进行展示--对应的是Template模板。
(貌似也可以反过来理解,首先前台发起请求,然后通过view.py文件对请求做响应处理,接着在数据库(model.py)中进行数据的查询)

了解了大致原理,我们来进行代码的编写。
[参考文章链接]
(https://blog.csdn.net/qq_41989320/article/details/84308565, https://www.cnblogs.com/harryblog/p/9374206.html)

1.首先是模型文件(Models文件)的处理。在之前的文章中有介绍过链接地址https://www.jianshu.com/p/b9b080439d78),大致就是数据文件的迁移(生成数据库),然后生成大量数据(用来做后续的查询展示处理)。
2.在视图文件(Views文件)中编写分页方法。

from django.core.paginator import Paginator, PageNotAnInteger, InvalidPage, EmptyPage
from .models import User #从models.py文件中引入数据表
def pagination(request):
    user_list = User.objects.all() #查询数据表中的全部元素
    paginator = Paginator(user_list, 8) #对数据进行分页处理,第一个参数是一个列表类型,第二个参数是吗,每页展示数据的条数 

    current_page_num = int(request.GET.get("page", 1)) #获取当前所在页

#这里进行逻辑的判断,进行总页数的显示,判断上下页是否存在等等
   if paginator.num_pages > 11:
        if current_page_num-5 < 1:
            page_range = range(1, 11)
        elif current_page_num+5 > paginator.num_pages:
            page_range = range(paginator.num_pages-11, paginator.num_pages+1)
        else:
            page_range = range(current_page_num-5, current_page_num+6)
    else:
        page_range = paginator.page_range
    try:
        current_page = paginator.page(current_page_num) #创建一个页面的page对象,每一个page对应一个页面
#page对象有三个属性:
#page.number:表示当前查询的页码
#page.object_list:表示当前页要展示的数据
#page.paginator:它是上面创建的paginator = Paginator(user_list, 8)对象,无论那一页,始终跟随着page对象
    except:
        current_page = paginator.page(1) #出现异常默认展示第一页
    return render(request, 'show.html', locals()) #local()函数用来传递参数,作用是返回字典类型的局部变量,可以一次性传递当前函数(pagination)的所有变量

3.模板文件的编写

<div id="d2">
<table>
    <thead>
    <th>序  号</th>
    <th>用户名</th>
    <th>密  码</th>
    <th>身份证</th>
    <th>地  址</th>
    <th>电  话</th>
    <th>预约号</th>
    </thead>
    {% for user in current_page.object_list %}
    <tr>
        <td>{{ user.id }}</td>
        <td>{{ user.username }}</td>
        <td>{{ user.password }}</td>
        <td>{{ user.id_number }}</td>
        <td>{{ user.address }}</td>
        <td>{{ user.phone_number }}</td>
        <td>{{ user.reserve_number }}</td>
    </tr>
    {% endfor %}
</table>

<div id="d3">
    {% if current_page.has_previous %}
        <a href="?page={{ current_page_num | add:-1 }}" aria-label="Previous">
            <span aria-hidden="true">&laquo; 上一页</span>
        </a>
    {% else %}
        <a href="" aria-label="Previous">
            <span aria-hidden="true">&laquo; 上一页</span>
        </a>
    {% endif %}
        {% for item in page_range %}
            {% if current_page_num == item %}
                <a href="?page={{ item }}">{{ item }}</a>
            {% else %}
                <a href="?page={{ item }}">{{ item }}</a>
            {% endif %}
        {% endfor %}
    {% if current_page.has_next %}
        <a href="?page={{ current_page_num | add:1 }}" aria-label="Next">
            <span aria-hidden="true">下一页 &raquo;</span>
        </a>
    {% endif %}
</div>

模板文件样式

<style>
 * {
        margin:0;
        padding:0;
    }
    #d2{
       color:white;
       background:#2C778A;
       margin:0 auto;
       margin-top:60px;
       width: 550px;
    }
    #d3{
    color:white;
    width:550px;
    height:30px;
    margin: 0 auto;
    text-align:center;
    }
    #d2 table{
     text-align:center;
     margin:0 auto;
     padding:20px;
    }
    a{
        text-decoration:none;
    }
    a:link{
        color:white;
    }
    a:visited{
        color:white;
    }
</style>

4.本地路由和全局路由的配置

#本地路由文件
from django.urls import path
from . import views
urlpatterns = [
    path('', views.pagination),
]
#全局路由文件
from django.contrib import admin
from django.urls import include, path
from index import views

urlpatterns = [
    path('show/', include("apps.urls")),
    path('admin/', admin.site.urls),
]

好了,到这里全部代码就编写完成了,以上就完成了一个简单的分页展示功能
参考文章链接
1.https://blog.csdn.net/qq_41989320/article/details/84308565
2.https://www.cnblogs.com/harryblog/p/9374206.html

上一篇 下一篇

猜你喜欢

热点阅读