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>

 效果如下:

posted @   Mr'liu  阅读(958)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2020-07-20 Mysql数据库意外崩溃导致表数据文件损坏无法启动的问题解决
点击右上角即可分享
微信分享提示