Python Django 页面上展示固定的页码数实现代码

yipeiwu_com6年前Python基础

如果页数太多的话,全部显示在页面上就会显得很冗杂

可以在页面中显示规定的页码数

例如:

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">
      <li>
        <a href="#" rel="external nofollow" rel="external nofollow" aria-label="Previous">
          <span aria-hidden="true">«</span>
        </a>
      </li>
      <li>
        {{ page_html|safe }}
      </li>
      <li>
        <a href="#" rel="external nofollow" rel="external nofollow" aria-label="Next">
          <span aria-hidden="true">»</span>
        </a>
      </li>
    </ul>
  </nav> 
</div> 
</body>
</html>

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)
 
  # 页面上最多展示的页码
  max_page = 11
  half_max_page = max_page // 2
 
  # 页面上展示的页码的开始页
  page_start = page_num - half_max_page
  # 页面上展示的页码的结束页
  page_end = page_num + half_max_page
 
  # 如果当前页减一半比 1 小
  if page_start <= 1:
    page_start = 1
    page_end = max_page
  # 如果当前页加一半比总页码还大
  if page_end > total_page:
    page_end = total_page
    page_start = total_page - max_page + 1
 
  # 如果还有数据
  if m:
    total_page += 1
 
  all_book = models.Book.objects.all()[data_start:data_end]
 
  # 拼接 html 的分页代码
  html_list = []
  for i in range(page_start, page_end+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})

运行结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python中break、continue 、exit() 、pass终止循环的区别详解

python中break、continue 、exit() 、pass终止循环的区别详解

python中break、continue 、exit() 、pass区分 1、break:跳出循环,不再执行 Python break语句,就像在C语言中,打破了最小封闭for或...

利用django如何解析用户上传的excel文件

前言 我们在工作中的时候,会有这种需求:用户上传一个格式固定excel表格到网站上,然后程序负债解析内容并进行处理。我最近在工作中就遇到了,所以想着将解决的过程总结分享出来,方便大家参考...

Django-Rest-Framework 权限管理源码浅析(小结)

Django-Rest-Framework 权限管理源码浅析(小结)

在django的views中不论是用类方式还是用装饰器方式来使用rest框架,django_rest_frame实现权限管理都需要两个东西的配合: authentication_clas...

Django restframework 源码分析之认证详解

Django restframework 源码分析之认证详解

前言 最近学习了 django 的一个 restframework 框架,对于里面的执行流程产生了兴趣,经过昨天一晚上初步搞清楚了执行流程(部分方法还不太清楚),于是想详细的总结一下当来...

Python rstrip()方法实例详解

Python 字符串 描述 Python rstrip() 删除 string 字符串末尾的指定字符(默认为空格). 语法 rstrip()方法语法: str.rstrip([chars...