flask_paginate对mysql数据进行分页查询
使用flask_paginate对mysql数据进行分页查询,这里介绍使用原生sql方法,也可以使用SQLAlchemy对数据做查询
1 | app.py |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | from flask import Flask,render_template,redirect from flask import request from db_helper import MySQL from flask_paginate import Pagination,get_page_parameter app = Flask(__name__) @app .route( '/test/' , defaults = { 'page' : 1 }) @app .route( '/test/<page>' ) def test(page): page = request.args.get(get_page_parameter(), type = int , default = int (page)) dbconn = MySQL(host = '10.10.10.1' , user = 'root' , passwd = 'passwd' , db = 'test' , port = 3306 , charset = "utf8mb4" ) select_sql = "select * from server" # 分页数 page_num = 5 filter_sql = ( " ORDER BY id ASC LIMIT {limit} offset {offset}" . format (limit = page_num, offset = (page_num * int (page) - page_num))) ret_sql = select_sql + filter_sql table_data = dbconn.fetch_rows(ret_sql, as_dict = True ) table_data_total = len (dbconn.fetch_rows(select_sql, as_dict = True )) paginate = Pagination(page = page, total = table_data_total,per_page = page_num) return render_template( 'test.html' , table_ret = table_data, paginate = paginate) if __name__ = = "__main__" : app.run() |
1 | ## https://www.cnblogs.com/liucx/ |
1 | test.html |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <div class = "col" > <table class = "table table-bordered table-hover" > <thead> <th>服务器名< / th> <th>服务器IP< / th> <th>服务器端口< / th> <th>服务器用户< / th> <th>操作< / th> < / thead> <tbody id = "tbodycontent" > { % if table_ret % } { % for row in table_ret % } <tr> <td> {{ row.name }}< / td> <td> {{ row.ip }}< / td> <td> {{ row.port }}< / td> <td> {{ row.user }}< / td> <td> < / td> < / tr> { % endfor % } { % endif % } < / tbody> < / table> <div class = "page_footer" ><span class = "page_total" >共 {{ paginate.total }} 条< / span> {{ paginate.links }}< / div> < / div> |
效果如下:
作者:Liucx
E-mail:57349220@qq.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2020-07-20 Mysql数据库意外崩溃导致表数据文件损坏无法启动的问题解决