这篇文章主要介绍了Python Django 添加首页尾页上一页下一页代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
添加首页和尾页:
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 = []

添加首页按钮

html_list.append(‘

  • 首页
  • ’)

    展示的页码

    for i in range(page_start, page_end + 1):
    tmp = ‘

  • {0}
  • ’.format(i)
    html_list.append(tmp)

    添加尾页按钮

    html_list.append(‘

  • 尾页
  • ’.format(total_page))
    page_html = “”.join(html_list) # 拼接 html 的分页代码
    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" 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>{{ page_html|safe }}</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) * 10data_end = page_num * 10# 书籍总数total_count = models.Book.objects.all().count() # 每一页显示多少条数据per_page = 10# 总共需要多少页码来显示total_page, m = divmod(total_count, per_page) # 页面上最多展示的页码max_page = 11half_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 = 1page_end = max_page# 如果当前页加一半比总页码还大if page_end > total_page:page_end = total_pagepage_start = total_page - max_page + 1# 如果还有数据if m:total_page += 1all_book = models.Book.objects.all()[data_start:data_end] # 拼接 html 的分页代码html_list = [] # 添加首页按钮html_list.append('<li><a href="/book_list/?page=1" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首页</a></li>') # 如果是第一页,就没有上一页if page_num <= 1:html_list.append('<li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">«</span></a></li>'.format(page_num-1))else:# 加一个上一页的标签html_list.append('<li><a href="/book_list/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">«</span></a></li>'.format(page_num-1)) # 展示的页码for i in range(page_start, page_end + 1):# 给当前页添加 activeif i == page_num:tmp = '<li class="active"><a href="/book_list/?page={0}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{0}</a></li>'.format(i)else:tmp = '<li><a href="/book_list/?page={0}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{0}</a></li>'.format(i)html_list.append(tmp) # 如果是最后一页,就没有下一页if page_num >= total_page:html_list.append('<li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">»</span></a></li>')else:html_list.append('<li><a href="/book_list/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">»</span></a></li>'.format(page_num+1)) # 添加尾页按钮html_list.append('<li><a href="/book_list/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾页</a></li>'.format(total_page))page_html = "".join(html_list) # 拼接 html 的分页代码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" 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>{{ page_html|safe }}</li></ul></nav>
    </div>
    </body>
    </html>
    

    运行结果:
    后续改进:

    处理用户传给 url 的 page 参数异常的值的情况

    例如:

    访问,http://127.0.0.1:8888/book_list/?page=a

    访问,http://127.0.0.1:8888/book_list/?page=-1

    都会出错

    改进:

    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 为 str 类型# 书籍总数total_count = models.Book.objects.all().count() # 每一页显示多少条数据per_page = 10# 总共需要多少页码来显示total_page, m = divmod(total_count, per_page) # 如果还有数据if m:total_page += 1try:page_num = int(page_num)# 如果输入的页码数超过了最大的页码数,默认返回最后一页if page_num > total_page:page_num = total_page# 如果输入的页码数小于 1,则返回第一页if page_num < 1:page_num = 1except Exception as e:# 当输入的页码不是正经数字的时候 默认返回第一页的数据page_num = 1# 定义两个变量保存数据从哪儿取到哪儿data_start = (page_num - 1) * 10data_end = page_num * 10# 页面上最多展示的页码max_page = 11half_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 = 1page_end = max_page# 如果当前页加一半比总页码还大if page_end > total_page:page_end = total_pagepage_start = total_page - max_page + 1all_book = models.Book.objects.all()[data_start:data_end] # 拼接 html 的分页代码html_list = [] # 添加首页按钮html_list.append('<li><a href="/book_list/?page=1" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首页</a></li>') # 如果是第一页,就没有上一页if page_num <= 1:html_list.append('<li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">«</span></a></li>'.format(page_num-1))else:# 加一个上一页的标签html_list.append('<li><a href="/book_list/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">«</span></a></li>'.format(page_num-1))# 展示的页码for i in range(page_start, page_end + 1):# 给当前页添加 activeif i == page_num:tmp = '<li class="active"><a href="/book_list/?page={0}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{0}</a></li>'.format(i)else:tmp = '<li><a href="/book_list/?page={0}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{0}</a></li>'.format(i)html_list.append(tmp) # 如果是最后一页,就没有下一页if page_num >= total_page:html_list.append('<li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">»</span></a></li>')else:html_list.append('<li><a href="/book_list/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">»</span></a></li>'.format(page_num+1)) # 添加尾页按钮html_list.append('<li><a href="/book_list/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾页</a></li>'.format(total_page))page_html = "".join(html_list) # 拼接 html 的分页代码return render(request, "book_list.html", {"books": all_book, "page_html": page_html})
    

    如果数据库中的数据数少于 max_page,则会显示负数的页数

    例如数据库中只有 21 条数据:
    改进:

    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 为 str 类型 # 书籍总数total_count = models.Book.objects.all().count() # 每一页显示多少条数据per_page = 10# 总共需要多少页码来显示total_page, m = divmod(total_count, per_page) # 如果还有数据if m:total_page += 1try:page_num = int(page_num)# 如果输入的页码数超过了最大的页码数,默认返回最后一页if page_num > total_page:page_num = total_page# 如果输入的页码数小于 1,则返回第一页if page_num < 1:page_num = 1except Exception as e:# 当输入的页码不是正经数字的时候 默认返回第一页的数据page_num = 1# 定义两个变量保存数据从哪儿取到哪儿data_start = (page_num - 1) * 10data_end = page_num * 10# 页面上最多展示的页码max_page = 11# 如果总页码数小于页面上最多展示的页码if total_page < max_page:max_page = total_pagehalf_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 = 1page_end = max_page# 如果当前页加一半比总页码还大if page_end > total_page:page_end = total_pagepage_start = total_page - max_page + 1all_book = models.Book.objects.all()[data_start:data_end] # 拼接 html 的分页代码html_list = [] # 添加首页按钮html_list.append('<li><a href="/book_list/?page=1" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首页</a></li>') # 如果是第一页,就没有上一页if page_num <= 1:html_list.append('<li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">«</span></a></li>'.format(page_num-1))else:# 加一个上一页的标签html_list.append('<li><a href="/book_list/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">«</span></a></li>'.format(page_num-1))# 展示的页码for i in range(page_start, page_end + 1):# 给当前页添加 activeif i == page_num:tmp = '<li class="active"><a href="/book_list/?page={0}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{0}</a></li>'.format(i)else:tmp = '<li><a href="/book_list/?page={0}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{0}</a></li>'.format(i)html_list.append(tmp) # 如果是最后一页,就没有下一页if page_num >= total_page:html_list.append('<li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">»</span></a></li>')else:html_list.append('<li><a href="/book_list/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">»</span></a></li>'.format(page_num+1)) # 添加尾页按钮html_list.append('<li><a href="/book_list/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾页</a></li>'.format(total_page)) page_html = "".join(html_list) # 拼接 html 的分页代码return render(request, "book_list.html", {"books": all_book, "page_html": page_html})
    

    运行结果:

    最后给大家推荐一个资源很全的python学习聚集地,[点击进入],这里有我收集以前学习心得,学习笔记,还有一线企业的工作经验,且给大定on零基础到项目实战的资料,大家也可以在下方,留言,把不懂的提出来,大家一起学习进步

Python Django 添加首页尾页上一页下一页代码实例相关推荐

  1. 在当当买了python怎么下载源代码-Python爬取当当、京东、亚马逊图书信息代码实例...

    注:1.本程序采用MSSQLserver数据库存储,请运行程序前手动修改程序开头处的数据库链接信息 2.需要bs4.requests.pymssql库支持 3.支持多线程 from bs4 impor ...

  2. Python+django网页设计入门(16):优化设计复用分页代码

    前导课程: Python+django网页设计入门(15):公用模板设计与使用 Python+django网页设计入门(14):使用中间件对抗爬虫 Python+django网页设计入门(13):表单 ...

  3. 如何修改dede文章页上一篇下一篇"没有了"

    dedecms上一篇下一篇调用标签: {dede:prenext get='pre'/} {dede:prenext get='next'/} 如何自定义织梦dedecms上一篇下一篇标签调用内容呢, ...

  4. python控制软件点击_Python小程序 控制鼠标循环点击代码实例

    Python小程序 控制鼠标循环点击代码实例 这篇文章主要介绍了Python小程序 控制鼠标循环点击代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以 ...

  5. Python模拟登陆淘宝并统计淘宝消费情况的代码实例分享

    支付宝十年账单上的数字有点吓人,但它统计的项目太多,只是想看看到底单纯在淘宝上支出了多少,于是写了段脚本,统计任意时间段淘宝订单的消费情况,看那结果其实在淘宝上我还是相当节约的说. 脚本的主要工作是模 ...

  6. python数据导出excel_python 数据生成excel导出(xlwt,wlsxwrite)代码实例

    这篇文章主要介绍了python 数据生成excel导出(xlwt,wlsxwrite)代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 话不多 ...

  7. python简单爬虫手机号_python手机号前7位归属地爬虫代码实例

    需求分析 项目上需要用到手机号前7位,判断号码是否合法,还有归属地查询.旧的数据是几年前了太久了,打算用python爬虫重新爬一份 单线程版本 # coding:utf-8 import reques ...

  8. python图像转字符画_Python实现图片转字符画的代码实例

    如何利用Python实现图片转字符画详解 如何用python将图片转为字符画给小编你的心作纪念,小编的梦有你的祝福才能够完全,风浪再大,小编也会勇往直前,小编们的爱,镶在青春的纪念册. # codin ...

  9. mysql创建临时表 分页_ASP+MySQl利用临时表分页第一页数据正常,下一页往后没有数据...

    具体代码如下,那位高手帮解决下: 具体代码如下,那位高手帮解决下: '初始化数据库连接 strConnection = "dsn=mydata;driver={myodbd driver}; ...

  10. python list join函数_Python中join()函数多种操作代码实例

    这篇文章主要介绍了Python中join()函数多种操作代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Python中有.join()和os ...

最新文章

  1. Vue使用watch监听一个对象中的属性
  2. 框架:mybatis的缓存机制
  3. jdbc mysql 报错 ssl_Mybatis使用JDBC连接数据库报错及解决方案
  4. linux 用mutex定义一个linkedlist,一个高性能无锁非阻塞链表队列
  5. leetcode744. 寻找比目标字母大的最小字母(二分查找)
  6. 「最有用」的特殊大数据:一文看懂文本信息系统的概念框架及功能
  7. 怎么在html页面和js里判断是否是IE浏览器
  8. should be mapped with insert=false update=false
  9. selenium+python自动化,数据驱动实例
  10. 如何在iPhone、iPad和Mac上查看照片的EXIF数据?
  11. httpget和ajax,javascript - HTTP中的get、post请求和ajax的get、post请求是一个东西吗?...
  12. HTML5+js+css3开心消消乐手机pc端通用源码|H5小游戏
  13. Libevent 源码文件结构分析
  14. 影视后期制作(Pr)
  15. win10红警2黑屏_win10怎么玩红警2尤里的复仇及其mod的方法汇总,mod打不开、卡顿的通用解决方案...
  16. FPGA实现实时运动目标检测verilog
  17. Java元数据区的概念_JVM的元数据区
  18. 计算机vfp题库知识,全国计算机vfp二级考试题库(超全).docx
  19. Defender绝密档案:惊现中本聪?
  20. sec^3 不定积分

热门文章

  1. wordpress Avada主题banner制作
  2. python word表格操作_Python|处理word的基本操作
  3. JetBrain系列好用的插件
  4. iOS 1 到 iOS 10 ,我都快老了
  5. 苹果系统中国日历服务器,简单三步,让 iPhone 自带日历 App 显示国家节假日安排...
  6. Win系统使用WSL子系统Linux启动vGPU增强图形性能加速OpenGL
  7. 联想服务器怎么拆硬盘,联想ThinkStation P900工作站高清拆解
  8. 清空计算机网络缓存,怎么清除DNS缓存 利用命令行清理dns缓存方法
  9. 【XJTUSE计算机图形学】第三章 几何造型技术(3)——B样条曲线与曲面
  10. Python绘制六边形