Flask 实现分页
pager.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div class="container">
<div class="row " style="margin-top: 10px">
<ul>
{% for foo in index_list %}
<li>{{ foo }}</li>
{% endfor %}
</ul>
<nav aria-label="Page navigation" class="pull-right">
<ul class="pagination">
{{ html|safe }}
</ul>
</nav>
</div>
</div>
</body>
</html>
pager.py
from urllib.parse import urlencode,quote,unquote
class Pagination(object):
def __init__(self,current_page,total_count,base_url,params,per_page_count=10,max_pager_count=11):
try:
current_page = int(current_page)
except Exception as e:
current_page = 1
if current_page <=0:
current_page = 1
self.current_page = current_page
# 数据总条数
self.total_count = total_count
# 每页显示10条数据
self.per_page_count = per_page_count
# 页面上应该显示的最大页码
max_page_num, div = divmod(total_count, per_page_count)
if div:
max_page_num += 1
self.max_page_num = max_page_num
# 页面上默认显示11个页码(当前页在中间)
self.max_pager_count = max_pager_count
self.half_max_pager_count = int((max_pager_count - 1) / 2)
# URL前缀
self.base_url = base_url
# request.GET
import copy
params = copy.deepcopy(params)
# params._mutable = True
get_dict = params.to_dict()
# 包含当前列表页面所有的搜/索条件
# {source:[2,], status:[2], gender:[2],consultant:[1],page:[1]}
# self.params[page] = 8
# self.params.urlencode()
# source=2&status=2&gender=2&consultant=1&page=8
# href="/hosts/?source=2&status=2&gender=2&consultant=1&page=8"
# href="%s?%s" %(self.base_url,self.params.urlencode())
self.params = get_dict
@property
def start(self):
return (self.current_page - 1) * self.per_page_count
@property
def end(self):
return self.current_page * self.per_page_count
def page_html(self):
# 如果总页数 <= 11
if self.max_page_num <= self.max_pager_count:
pager_start = 1
pager_end = self.max_page_num
# 如果总页数 > 11
else:
# 如果当前页 <= 5
if self.current_page <= self.half_max_pager_count:
pager_start = 1
pager_end = self.max_pager_count
else:
# 当前页 + 5 > 总页码
if (self.current_page + self.half_max_pager_count) > self.max_page_num:
pager_end = self.max_page_num
pager_start = self.max_page_num - self.max_pager_count + 1 #倒这数11个
else:
pager_start = self.current_page - self.half_max_pager_count
pager_end = self.current_page + self.half_max_pager_count
page_html_list = []
# {source:[2,], status:[2], gender:[2],consultant:[1],page:[1]}
# 首页
self.params['page'] = 1
first_page = '<li><a href="%s?%s">首页</a></li>' % (self.base_url,urlencode(self.params),)
page_html_list.append(first_page)
# 上一页
self.params["page"] = self.current_page - 1
if self.params["page"] < 1:
pervious_page = '<li class="disabled"><a href="%s?%s" aria-label="Previous">上一页</span></a></li>' % (self.base_url, urlencode(self.params))
else:
pervious_page = '<li><a href = "%s?%s" aria-label = "Previous" >上一页</span></a></li>' % ( self.base_url, urlencode(self.params))
page_html_list.append(pervious_page)
# 中间页码
for i in range(pager_start, pager_end + 1):
self.params['page'] = i
if i == self.current_page:
temp = '<li class="active"><a href="%s?%s">%s</a></li>' % (self.base_url,urlencode(self.params), i,)
else:
temp = '<li><a href="%s?%s">%s</a></li>' % (self.base_url,urlencode(self.params), i,)
page_html_list.append(temp)
# 下一页
self.params["page"] = self.current_page + 1
if self.params["page"] > self.max_page_num:
self.params["page"] = self.current_page
next_page = '<li class="disabled"><a href = "%s?%s" aria-label = "Next">下一页</span></a></li >' % (self.base_url, urlencode(self.params))
else:
next_page = '<li><a href = "%s?%s" aria-label = "Next">下一页</span></a></li>' % (self.base_url, urlencode(self.params))
page_html_list.append(next_page)
# 尾页
self.params['page'] = self.max_page_num
last_page = '<li><a href="%s?%s">尾页</a></li>' % (self.base_url, urlencode(self.params),)
page_html_list.append(last_page)
return ''.join(page_html_list)
app.py
from flask import Flask,render_template,request,redirect
from pager import Pagination
from urllib.parse import urlencode
app = Flask(__name__)
@app.route('/pager')
def pager():
li = []
for i in range(1,100):
li.append(i)
pager_obj = Pagination(request.args.get("page",1),len(li),request.path,request.args,per_page_count=10)
index_list = li[pager_obj.start:pager_obj.end]
html = pager_obj.page_html()
return render_template("pager.html",index_list=index_list, html = html)
if __name__ == '__main__':
app.run(debug=True)
Flask 实现分页的更多相关文章
- 4、flask之分页插件的使用、添加后保留原url搜索条件、单例模式
本篇导航: flask实现分页 添加后保留原url搜索条件 单例模式 一.flask实现分页 1.django项目中写过的分页组件 from urllib.parse import urlencode ...
- flask之分页插件的使用、添加后保留原url搜索条件、单例模式
本篇导航: flask实现分页 添加后保留原url搜索条件 单例模式 一.flask实现分页 1.django项目中写过的分页组件 from urllib.parse import urlencode ...
- flask 前端 分页 显示
# flask 前端 分页 显示 1.分页原理 web查询大量数据并显示时有有三种方式: 从数据库中查询全部,在view/客户端筛选/分页:不能应对记录大多的情况,一般不使用: 分页查询,每次在数据库 ...
- Flask系列(五)Flask实现分页
一.flask分页组件 from urllib.parse import urlencode,quote,unquote class Pagination(object): ""& ...
- flask的分页功能
分页是个很通用的东西,在flask中,有一个macro的语法,类似于宏,我们可以将通用的东西通过macro写入单独的html文件以方便维护,减少代码量.下面是我的分页的macro文件render_pa ...
- Flask实现分页功能
可以参考: https://blog.csdn.net/weixin_36380516/article/details/80295101 也可以参考我的代码: https://github.com/z ...
- Flask构建微电影(一)
第一章.项目介绍 1.1.前言 本教程我将带领大家如何使用flask框架开发微电影网站.Flask是python中最受欢迎的轻量级web框架,flask扩展丰富,冗余度小,可自由选择组合各种插 ...
- Flask开发微电影网站(一)
1.用到的Flask知识 1.使用整形,浮点型,路径型,字符串型下正则表达式路由转化器 2.使用GET与POST请求,上传文件,cookie获取与响应,404处理 3.使用模板自动转义,定义过滤器,定 ...
- python操作三大主流数据库(4)python操作mysql④python服务端flask和前端bootstrap框架结合实现新闻展示
python操作mysql④python服务端flask和前端bootstrap框架结合实现新闻展示 参考文档http://flask.pocoo.org/docs/0.11/http://flask ...
随机推荐
- Java I/O流 02
IO流·字节流 IO流概述及其分类 * A:概念 * IO流用来处理设备之间的数据传输 * Java对数据的操作是通过流操作的 * Java用于操作流的类都在IO包中 * 流按流向分为两种输入流.输出 ...
- 关于PHP的表单提交显示
实现功能:html页面,向表单内填入账号.密码:php页面,将填写的账号与密码信息展示出来. demo6.html代码如下: 1 <!DOCTYPE html> 2 <html& ...
- 从零学脚手架(三)---webpack属性详解
如果此篇对您有所帮助,在此求一个star.项目地址: OrcasTeam/my-cli 在上一篇中,介绍了webpack的entry.output.plugins属性. 在这一篇,接着介绍其它配置属性 ...
- Python爬虫学习一------HTTP的基本原理
昨天刚买的崔大大的<Python3网络爬虫开发实战>,今天就到了,开心的读完了爬虫基础这一章,现记录下自己的浅薄理解,如有见解不到位之处,望指出. 1.HTTP的基本原理 ①我们经常会在浏 ...
- 数据库Redis(一)
Redis数据库的特点: Redis数据库属于nosql数据库的一种,其存储于内存中(非硬盘),修改较为方便. 而Redis数据库的存储方式是使用{key:value}方式存储,类似python基础中 ...
- PTE 准备之 Describe Image
25s 准备时间:决定用什么模板,用模板cover那些信息点 Content: 数字和文字哪个多,就多说哪个,均匀覆盖 Fluency : 保持流利度 不要纠结时态,单复数,人称代词等 时间要求: 尽 ...
- java重写toString()方法
toString()方法是Object类的方法,调用toString()会返回对象的描述信息. 1)为什么重写toString()方法呢? 如果不重写,直接调用Object类的toString()方法 ...
- kmp&字典树 模板
kmp: const int maxn=1e5+5; char s[maxn],p[maxn]; int nex[maxn]; int KmpSearch(char* s, char* p) { in ...
- Windows下解析命令行参数
linux通常使用GNU C提供的函数getopt.getopt_long.getopt_long_only函数来解析命令行参数. 移植到Windows下 getopt.h #ifndef _GETO ...
- ABP 适用性改造 - 精简 ABP CLI 生成的项目结构
Overview 不管是公司或者个人都会有不同的开发习惯,通过建立项目模板,既可以使开发人员聚焦于业务功能的开发,也可以在一定程度上统一不同开发人员之间的开发风格.在使用 ABP 框架的过程中,对于 ...