美文网首页
关于Django框架中分页器的使用

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

作者: 脏脏的小泥娃 | 来源:发表于2020-03-14 10:35 被阅读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

相关文章

  • 第20天,分页器

    本篇写了使用Django自有的分页器paginator的用法和自定制分页器 1.1 Django之分页功能 Dj...

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

    最近搬码的时候需要在Django框架中用到分页功能,于是就去百度了一下。 django框架中实现分页功能可以有很多...

  • 分页-Django REST框架

    分页-Django REST框架 pagination.py 分页 Django提供了几个类来帮助您管理分页数据-...

  • Django分页

    我们要实现如下样子的分页界面 在不借助于第三方Django APP的情况下,仅仅使用Django框架提供的分页(P...

  • django 使用原生sql及分页

    python3 django 使用原生sql及分页 django 使用原生sql及分页,类似于django res...

  • Django框架的使用(一)

    Django框架的使用 pip list查看python中是否安装了Django框架,如果列表中包含Django则...

  • Flask ----- 前端页面分页器对象

    Flask和Django都有的分页器类 Pageinations 分页器对象属性和方法 分页流程示例 借书管理系统...

  • Django之分页器

    Django自带分页器的实现 介绍 Django提供了一个新的类来帮助你管理分页数据,这个模块存放在django....

  • 分页

    分页 分页库Paginator的基本语法 django提供了分页的工具,存在于django.core中 Pagin...

  • rest_framework 分页

    和django一样,rest_framework也有自己的分页器,且rest_framework的分页器是基于dj...

网友评论

      本文标题:关于Django框架中分页器的使用

      本文链接:https://www.haomeiwen.com/subject/kgaddhtx.html