Python Django 简单分页的实现代码解析

作者:Sch01aR# 时间:2021-11-05 13:27:38 

这篇文章主要介绍了Python Django 简单分页的实现代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

models.py:


from django.db import models
class Book(models.Model):
 title = models.CharField(max_length=32)
 def __str__(self):
   return self.title
 class Meta:
   db_table = "books"

批量创建 106 条数据


import os
if __name__ == '__main__':
 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite3.settings")
 import django
 django.setup()
 from app01 import models
 # 106 个书籍对象
 objs = [models.Book(title="《Python 的故事第{}版》".format(i)) for i in range(116)]
 # 在数据库中批量创建, 10 次一提交
 models.Book.objects.bulk_create(objs, 10)

views.py:


from django.shortcuts import render
from app01 import models
def book_list(request):
 # 从 URL 中取参数
 page_num = request.GET.get("page")
 print(page_num, type(page_num))
 page_num = int(page_num)

# 定义两个变量保存数据从哪儿取到哪儿
 data_start = (page_num-1)*10
 data_end = page_num*10

# 书籍总数
 total_count = models.Book.objects.all().count()

# 每一页显示多少条数据
 per_page = 10

# 总共需要多少页码来显示
 total_page, m = divmod(total_count, per_page)
 if m:
   total_page += 1
 all_book = models.Book.objects.all()[data_start:data_end]

# 拼接 html 的分页代码
 html_list = []
 for i in range(1, total_page+1):
   tmp = '<li><a href="/book_list/?page={0}" rel="external nofollow" >{0}</a></li>'.format(i)
   html_list.append(tmp)
 page_html = "".join(html_list)
 return render(request, "book_list.html", {"books": all_book, "page_html": page_html})

book_list.html:


<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>书籍列表</title>
 <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css" rel="external nofollow" >
</head>
<body>
<div class="container">
 <table class="table table-bordered">
   <thead>
   <tr>
     <th>序号</th>
     <th>id</th>
     <th>书名</th>
   </tr>
   </thead>
   <tbody>
   {% for book in books %}
     <tr>
       <td>{{ forloop.counter }}</td>
       <td>{{ book.id }}</td>
       <td>{{ book.title }}</td>
     </tr>
   {% endfor %}
   </tbody>
 </table>
 <nav aria-label="Page navigation">
   <ul class="pagination">
     {{ page_html|safe }}
   </ul>
 </nav>
</div>
</body>
</html>

运行结果:

Python Django 简单分页的实现代码解析

来源:https://www.cnblogs.com/sch01ar/p/11328555.html

标签:python,django,简单,分页,实现
0
投稿

猜你喜欢

  • SQL Server数据库搭建农村信息化的方案

    2009-01-23 14:16:00
  • Mootools 1.2教程(15)——滚动条(Slider)

    2008-12-09 17:35:00
  • Perl中的正则表达式介绍

    2023-08-11 21:14:25
  • IE及Opera浏览器兼容笔记

    2008-08-21 17:53:00
  • 基于Three.js插件制作360度全景图

    2023-08-06 14:43:10
  • Python中的列表及其操作方法

    2022-05-24 06:19:41
  • python代码实现将列表中重复元素之间的内容全部滤除

    2023-11-17 18:17:32
  • 让大家看看Object标签的强大功能---多用途

    2009-02-21 10:18:00
  • ASP读取Exif信息无组件实现过程

    2009-02-09 12:52:00
  • php中替换字符串函数strtr()和str_repalce()的用法与区别

    2023-11-17 06:12:53
  • Python 数据可视化pyecharts的使用详解

    2021-07-07 20:29:55
  • oracle10g 数据备份与导入

    2009-06-10 18:21:00
  • MySQL数据库的其它安全问题

    2008-12-23 15:40:00
  • python中绑定方法与非绑定方法的实现示例

    2021-01-03 03:59:13
  • python 通过dict(zip)和{}的方式构造字典的方法

    2023-10-03 00:05:12
  • MYSQL创建触发程序的方法

    2009-07-30 08:38:00
  • pyshp创建shp点文件的方法

    2023-06-30 03:15:29
  • 下载golang.org/x包的操作方法

    2023-07-11 16:54:04
  • python 进程间数据共享multiProcess.Manger实现解析

    2021-03-25 14:06:53
  • discuz 跨域整合的记录文件

    2023-07-23 14:17:27
  • asp之家 网络编程 m.aspxhome.com