python flask实现分页的示例代码
作者:徐代龙 时间:2021-04-01 09:55:23
结合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.jb51.net/bootstrap/bootstrap-pagination.html
如果是返回给APP端的话,直接返回data数据就可以了。
来源:https://blog.csdn.net/xudailong_blog/article/details/80428013
标签:flask,分页
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
通过5个知识点轻松搞定Python的作用域
2021-10-02 03:51:23
Bootbox将后台JSON数据填充Form表单的实例代码
2023-08-22 22:01:21
![](https://img.aspxhome.com/file/2023/0/56090_0s.gif)
各种JavaScript开发工具比较
2007-10-23 13:29:00
![](/images/sg_trans.gif)
正则 global 属性介绍
2008-03-18 20:50:00
python str()如何将参数转换为字符串类型
2022-04-28 17:30:36
详解Python迭代和迭代器
2023-04-30 23:31:13
python的语句结构你真的了解吗
2022-08-11 23:05:39
python flask框架快速入门
2021-10-16 22:02:26
![](https://img.aspxhome.com/file/2023/0/89270_0s.jpg)
Python集合之set和frozenset的使用详解
2021-06-26 22:41:20
![](https://img.aspxhome.com/file/2023/4/68484_0s.png)
python快速编写单行注释多行注释的方法
2022-06-04 07:06:54
![](https://img.aspxhome.com/file/2023/9/78639_0s.jpg)
利用python设计图像加密技术(Arnold算法)
2021-09-19 11:24:18
![](https://img.aspxhome.com/file/2023/5/102735_0s.jpg)
Python使用RPC例子
2021-04-11 23:26:51
prototype.js常用函数及其用法
2007-11-21 20:47:00
Python多线程:主线程等待所有子线程结束代码
2021-03-18 05:31:53
面向对象的 Javascript
2013-07-21 11:57:22
JavaScript的陷阱
2008-10-28 19:52:00
Ubuntu20下的Django安装的方法步骤
2022-05-01 09:07:24
![](https://img.aspxhome.com/file/2023/5/81705_0s.png)
如何设计一个成功的网站
2007-09-07 10:33:00
FrontPage2002简明教程四:网页超级链接
2008-09-17 11:23:00
![](https://img.aspxhome.com/file/UploadPic/200810/17/20081017113025456s.jpg)
简单了解python的break、continue、pass
2022-06-01 01:20:10
![](https://img.aspxhome.com/file/2023/5/76975_0s.png)