python flask实现分页的示例代码


Posted in Python onAugust 02, 2018

结合mysql数据库查询,实现分页效果

@user.route("/user_list",methods=['POST','GET'])
def user_list():
  p = g.args.get("p", '') #页数
  show_shouye_status = 0 #显示首页状态

  if p =='':
    p=1
  else:
    p=int(p)
    if p > 1:
      show_shouye_status = 1

  mdb = db_session()
  limit_start = (int(p)-1)*10#起始

  sql ="select * from page_text limit {0},10".format(limit_start)
  user_list=mdb.getMany(sql)

  sql="select count(id) as total from page_text"
  count = mdb.getOne(sql)['total'] #总记录
  total = int(math.ceil(count/10.0)) #总页数

  dic = get_page(total,p)
  datas={
    'user_list':user_list,
    'p': int(p),
    'total': total,
    'show_shouye_status': show_shouye_status,
    'dic_list': dic

  }
  return render_template("user_list.html",datas=datas)

其中get_page为封装的方法:

def get_page(total,p):
  show_page = 5  # 显示的页码数
  pageoffset = 2 # 偏移量
  start = 1  #分页条开始
  end = total #分页条结束

  if total > show_page:
    if p > pageoffset:
      start = p - pageoffset
      if total > p + pageoffset:
        end = p + pageoffset
      else:
        end = total
    else:
      start = 1
      if total > show_page:
        end = show_page
      else:
        end = total
    if p + pageoffset > total:
      start = start - (p + pageoffset - end)
  #用于模版中循环
  dic = range(start, end + 1)
  return dic

如果这里需要进行前端模板的拼接的话,可以需要以下代码(bootstrap)

<ul class="pagination">
    {% if datas.show_shouye_status==1%}
      <li class=''><a href='/user/user_list?p=1'>首页</a></li>
      <li class=''><a href='/user/user_list?p={{datas.p-1}}'>上一页</a></li>
   {%endif%}

    {% for dic in datas.dic_list %}
      {% if dic==datas.p%}
       <li class="active"><a href="/user/user_list?p={{dic}}" rel="external nofollow" rel="external nofollow" >{{dic}}</a></li>
      {%else%}
        <li><a href="/user/user_list?p={{dic}}" rel="external nofollow" rel="external nofollow" >{{dic}}</a></li>
      {%endif%}
    {%endfor%}

    {% if datas.p < datas.total%}
      <li class=''><a href='/user/user_list?p={{datas.p+1}}'>下一页</a></li>
      <li class=''><a href='/user/user_list?p={{datas.total}}'>尾页</a></li>
    {%endif%}
      共{{datas.total}}页
 </ul>

bootstrap样式 http://edu.3water.com/bootstrap/bootstrap-pagination.html

如果是返回给APP端的话,直接返回data数据就可以了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python通过pip更新所有已安装的包实现方法
May 19 Python
python机器学习案例教程——K最近邻算法的实现
Dec 28 Python
Python内置模块logging用法实例分析
Feb 12 Python
python贪婪匹配以及多行匹配的实例讲解
Apr 19 Python
对Python3中bytes和HexStr之间的转换详解
Dec 04 Python
Python中函数的基本定义与调用及内置函数详解
May 13 Python
为什么从Python 3.6开始字典有序并效率更高
Jul 15 Python
浅析PEP572: 海象运算符
Oct 15 Python
python分布式计算dispy的使用详解
Dec 22 Python
python中pandas库中DataFrame对行和列的操作使用方法示例
Jun 14 Python
python中doctest库实例用法
Dec 31 Python
python使用matplotlib绘制图片时x轴的刻度处理
Aug 30 Python
Django分页查询并返回jsons数据(中文乱码解决方法)
Aug 02 #Python
Python实现正整数分解质因数操作示例
Aug 01 #Python
Python列表生成式与生成器操作示例
Aug 01 #Python
Python开发最牛逼的IDE——pycharm
Aug 01 #Python
django从请求到响应的过程深入讲解
Aug 01 #Python
python3.6的venv模块使用详解
Aug 01 #Python
从请求到响应过程中django都做了哪些处理
Aug 01 #Python
You might like
PHP数组对比函数,存在交集则返回真,否则返回假
2011/02/03 PHP
PHP实现的比较完善的购物车类
2014/12/02 PHP
PHP封装curl的调用接口及常用函数详解
2018/05/31 PHP
php如何把表单内容提交到数据库
2019/07/08 PHP
PHP接口类(interface)的定义、特点和应用示例
2020/05/18 PHP
关于取不到由location.href提交而来的上级页面地址的解决办法
2009/07/30 Javascript
通过Javascript将数据导出到外部Excel文档的函数代码
2012/06/15 Javascript
js将控件隐藏的方法及display属性介绍
2013/07/04 Javascript
javascript 实现键盘上下左右功能的小例子
2013/09/15 Javascript
JS获得选取checkbox整行数据的方法
2015/01/28 Javascript
Angular.js之作用域scope'@','=','&amp;'实例详解
2017/02/28 Javascript
IScroll5实现下拉刷新上拉加载的功能实例
2017/08/11 Javascript
js 发布订阅模式的实例讲解
2017/09/10 Javascript
在Vuex使用dispatch和commit来调用mutations的区别详解
2018/09/18 Javascript
JavaScript惰性载入函数实例分析
2019/03/27 Javascript
简单说说如何使用vue-router插件的方法
2019/04/08 Javascript
Handtrack.js库实现实时监测手部运动(推荐)
2021/02/08 Javascript
跟老齐学Python之编写类之二方法
2014/10/11 Python
flask框架实现连接sqlite3数据库的方法分析
2018/07/16 Python
python自制包并用pip免提交到pypi仅安装到本机【推荐】
2019/06/03 Python
python原类、类的创建过程与方法详解
2019/07/19 Python
python中的split()函数和os.path.split()函数使用详解
2019/12/21 Python
Python实现队列的方法示例小结【数组,链表】
2020/02/22 Python
Django models文件模型变更错误解决
2020/05/11 Python
关于keras中keras.layers.merge的用法说明
2020/05/23 Python
Python 图片处理库exifread详解
2021/02/25 Python
用HTML5制作数字时钟的教程
2015/05/11 HTML / CSS
英国No.1文具和办公用品在线:Euroffice
2016/09/21 全球购物
RetroStage德国:复古服装
2019/02/03 全球购物
公司前台接待岗位职责
2013/12/03 职场文书
优秀教师的感人事迹
2014/02/04 职场文书
老干部工作先进事迹
2014/08/17 职场文书
小学生放飞梦想演讲稿
2014/08/26 职场文书
小学母亲节活动总结
2015/02/10 职场文书
python 机器学习的标准化、归一化、正则化、离散化和白化
2021/04/16 Python
3050和2060哪个好 性能差多少 差距有多大 谁更有性价比
2022/06/17 数码科技