Django框架实现的分页demo示例

yipeiwu_com6年前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中的break、continue、exit()、pass全面解析

1、break break是终止本次循环,比如你很多个while循环,你在其中一个while循环里写了一个break,满足条件,只会终止这个while里面的循环,程序会跳到上一层whil...

Pyqt5实现英文学习词典

Pyqt5实现英文学习词典

运用Python语言编写程序制作英文学习词典,词典有4个基本功能:添加、查询、删除和退出。程序读取源文件路径下的txt格式词典文件,若没有就创建一个。词典文件存储方式为“英文单词 中文单...

遗传算法之Python实现代码

写在前面 之前的文章中已经讲过了遗传算法的基本流程,并且用MATLAB实现过一遍了。这一篇文章主要面对的人群是看过了我之前的文章,因此我就不再赘述遗传算法是什么以及基本的内容了,假设大家...

python实现mysql的单引号字符串过滤方法

本文实例讲述了python实现mysql的单引号字符串过滤方法。分享给大家供大家参考,具体如下: 最主要用这个函数,可以处理MySQLdb.escape_string(content)....

Python用于学习重要算法的模块pygorithm实例浅析

本文实例讲述了Python用于学习重要算法的模块pygorithm。分享给大家供大家参考,具体如下: 这是一个能够随时学习重要算法的Python模块,纯粹是为了教学使用。 特点 易...