Django框架实现的简单分页功能示例

作者:Yort2016 时间:2022-05-16 08:25:43 

本文实例讲述了Django框架实现的简单分页功能。分享给大家供大家参考,具体如下:

前面一篇《Django开发的简易留言板》写了个简单的留言板,如果数据量太多的话在一页显示就不那么友好了,本文就是做一个分页显示。

代码在上一篇的基础上修改。

导入分页模块并修改views


#只需修改index函数即可
from django.core.paginator import Paginator
def index(request):
 messages = models.Message.objects.all() #获取全部数据
 limit = 10
 paginator = Paginator(messages, limit) #按每页10条分页
 page = request.GET.get('page','1') #默认跳转到第一页
 result = paginator.page(page)
 return render(request, 'guestbook/index.html', {'messages' : result})

修改html


<!DOCTYPE html>
<html>
 <head>
   <meta charset="utf-8" />
   <title>留言板</title>
   <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="external nofollow" crossorigin="anonymous">
 </head>
 <body>
   <table class="table table-striped table-bordered table-hover table-condensed">
     <thead>
       <tr class="danger">
         <th>留言时间</th>
         <th>留言者</th>
         <th>标题</th>
         <th>内容</th>
       </tr>
     </thead>
     <tbody>
       {% if messages %}
         {% for message in messages %}
           <tr class="{% cycle 'active' 'success' 'warning' 'info' %}">
             <td>{{ message.publish|date:'Y-m-d H:i:s' }}</td>
             <td>{{ message.username }}</td>
             <td>{{ message.title }}</td>
             <td>{{ message.content }}</td>
           </tr>
         {% endfor %}
       {% else %}
         <tr>
           <td colspan="4">无数据</td>
         </tr>
       {% endif %}
     </tbody>
   </table>
   <!-- 分页开始 -->
   <div>
     <ul class="pagination">
     <li><a href="/guestbook/index/?page=1" rel="external nofollow" >首页</a></li>
        {% if messages.has_previous %}
           <li><a href="/guestbook/index/?page={{ messages.previous_page_number }}" rel="external nofollow" >上一页</a></li>
       {% endif %}
        {% for num in messages.paginator.page_range %}
         <li><a href="/guestbook/index/?page={{ num }}" rel="external nofollow" >{{ num }}</a></li>
        {% endfor %}
       {% if messages.has_next %}
          <li><a href="/guestbook/index/?page={{ messages.next_page_number }}" rel="external nofollow" >下一页</a></li>
       {% endif %}
       <li><a href="/guestbook/index/?page={{ messages.paginator.num_pages }}" rel="external nofollow" >尾页</a></li>
      </ul>
   </div>
   <!-- 分页结束 -->
   <div>
      <a class="btn btn-xs btn-primary" href="/guestbook/create/" rel="external nofollow" >去留言</a>
   </div>
 </body>
</html>

其实主要使用了Django自带的Paginator模块,关于这个模块大家可以自己去官方文档查看,功能还是挺强大的,如果配合ListView的话,三行代码就可以实现分页功能。

希望本文所述对大家基于Django框架的Python程序设计有所帮助。

来源:https://blog.csdn.net/Yort2016/article/details/73551274

标签:Django,分页
0
投稿

猜你喜欢

  • 界面设计10条可用性方面的启发

    2010-04-06 17:22:00
  • python轻松查到删除自己的微信好友

    2021-06-06 12:31:44
  • python matplotlib绘图,修改坐标轴刻度为文字的实例

    2023-09-29 12:27:57
  • python读取TXT每行,并存到LIST中的方法

    2023-08-09 06:09:20
  • Python3网络爬虫之使用User Agent和代理IP隐藏身份

    2022-03-05 13:13:07
  • python pandas中的agg函数用法

    2023-07-20 09:40:08
  • python中selenium库的基本使用详解

    2022-02-13 08:52:21
  • 详解Tensorflow不同版本要求与CUDA及CUDNN版本对应关系

    2021-01-09 10:40:27
  • Python实现K折交叉验证法的方法步骤

    2021-08-04 13:58:25
  • Python装饰器的应用场景及实例用法

    2022-06-24 16:09:03
  • Python中几种操作字符串的方法的介绍

    2021-06-16 22:50:06
  • Python实现运行其他程序的四种方式实例分析

    2023-08-03 00:48:24
  • Python实例详解递归算法

    2023-05-17 02:25:06
  • Dreamweaver制作技巧四则

    2008-01-04 09:42:00
  • 教你利用python实现企业微信发送消息

    2023-09-06 11:20:55
  • py中的目录与文件判别代码

    2023-06-01 03:32:06
  • css hack简易的“独享”与“交集”

    2008-08-31 20:17:00
  • 如何避免asp的SQL的执行效率低

    2009-01-08 18:18:00
  • Python3自定义http/https请求拦截mitmproxy脚本实例

    2021-04-13 15:33:01
  • python3 删除所有自定义变量的操作

    2023-09-26 03:26:08
  • asp之家 网络编程 m.aspxhome.com