Django框架实现的分页demo示例

yipeiwu_com5年前Python基础

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

首先初始化model,建表

class Book(models.Model):
  name = models.CharField(max_length=20)
  def __str__(self):
    return self.name
  class Meta:
    db_table = 'books'

然后用pycharm的数据库模块可视化插入

分页思路

url传递参数http://127.0.0.1:8000/books/?page=5比如这样传递的参数就是5,就显示第五页,

1.get到所有图书对象

2.计算好每一页应该有几个数据

3.根据不同的page值传递

def books(request):
  #取从url传递的参数
  page_num = request.GET.get('page')
  page_num = int(page_num)
  start = (page_num-1)*5
  end = page_num*5
  #总页码数是?
  per_page = 5
  total = models.Book.objects.all().count()
  total,more =divmod(total,per_page)
  if more:
    total+=1
  all_books = models.Book.objects.all()[start:end]
  #自己拼接分页的html代码
  html_str_list = []
  for i in range(1,total):
    tmp = '<li><a href="/books/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{}</li>'.format(i,i)
    html_str_list.append(tmp)
  page_html = "".join(html_str_list)
  return render(request,'books.html',{'books':all_books,'total_page':total,'page_html':page_html})

拿到数据总量的值,每一页的数量为5,如果有余数则total+1也就是增加一个页面.

建立一个列表,去拼接a标签,最后传递给前端

前端

前端的样式用到了boottrap,可以直接看文档.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>书记列表</title>
  <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.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.name }}</td>
      </tr>
    {% endfor %}
    </tbody>
  </table>
<nav aria-label="Page navigation">
 <ul class="pagination">
  <li>
   <a href="#" rel="external nofollow" rel="external nofollow" aria-label="Previous">
    <span aria-hidden="true">«</span>
   </a>
  </li>
   {{ page_html|safe }}
  <li>
   <a href="#" rel="external nofollow" rel="external nofollow" aria-label="Next">
    <span aria-hidden="true">»</span>
   </a>
  </li>
 </ul>
</nav>
</div>
</body>
</html>

{{ page_html|safe }}

传递过来的page_html要用safe过滤器,不然无法转移成html.

最终效果

分页优化

设置一个首页一个尾页,以及显示局部的页面

def books(request):
  # 取从url传递的参数
  page_num = request.GET.get('page')
  page_num = int(page_num)
  start = (page_num - 1) * 5
  end = page_num * 5
  # 总页码数是?
  per_page = 5
  # 页面上总共展示多少页面
  max_page = 11
  half_max_page = max_page // 2
  # 页面上展示的页面从哪开始
  page_start = page_num - half_max_page
  if page_start <= 1:
    page_start = 1
  total = models.Book.objects.all().count()
  # 页面到哪结束
  page_end = page_num+half_max_page
  if page_end > total:
    page_end = total
    page_start = total - max_page
  total, more = divmod(total, per_page)
  if more:
    total += 1
  all_books = models.Book.objects.all()[start:end]
  # 自己拼接分页的html代码
  html_str_list = []
  html_str_list.append('<li><a href="/books/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首页</li>'.format(1,1))
  for i in range(page_start, page_end+1):
    tmp = '<li><a href="/books/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{}</li>'.format(i, i)
    html_str_list.append(tmp)
  html_str_list.append('<li><a href="/books/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >最后一页</li>'.format(total))
  page_html = "".join(html_str_list)
  return render(request, 'books.html', {'books': all_books, 'total_page': total, 'page_html': page_html})

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

相关文章

Python实现PS滤镜的万花筒效果示例

Python实现PS滤镜的万花筒效果示例

本文实例讲述了Python实现PS滤镜的万花筒效果。分享给大家供大家参考,具体如下: 这里用 Python 实现 PS 的一种滤镜效果,称为万花筒。也是对图像做各种扭曲变换,最后图像呈现...

Anaconda2 5.2.0安装使用图文教程

Anaconda2 5.2.0安装使用图文教程

Anacond的介绍 Anaconda指的是一个开源的Python发行版本,其包含了conda、Python等180多个科学包及其依赖项。 因为包含了大量的科学包,Anacon...

Python数据结构与算法之图的最短路径(Dijkstra算法)完整实例

Python数据结构与算法之图的最短路径(Dijkstra算法)完整实例

本文实例讲述了Python数据结构与算法之图的最短路径(Dijkstra算法)。分享给大家供大家参考,具体如下: # coding:utf-8 # Dijkstra算法——通过边实现...

Python队列的定义与使用方法示例

Python队列的定义与使用方法示例

本文实例讲述了Python队列的定义与使用方法。分享给大家供大家参考,具体如下: 虽然Python有自己的队列模块,我们只需要在使用时引入该模块就行,但是为了更好的理解队列,自己将队列实...

python下的opencv画矩形和文字注释的实现方法

画矩形 函数调用:cv2.rectangle(img,pt1,pt2,color,thickness,line_type,shift) img: 图像. pt1: 矩形的一个顶点。 pt...