一、flask实现的分页组件

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)

二、使用组件

#!usr/bin/env python
# -*- coding:utf-8 -*-
from flask import Flask,render_template,request,redirect
from pager import Pagination
from urllib.parse import urlencode
app = Flask(__name__)

=========================django的用法=======================================
# pager_obj = Pagination(request.GET.get('page', 1), len(HOST_LIST), request.path_info, request.GET)
# host_list = HOST_LIST[pager_obj.start:pager_obj.end]
# html = pager_obj.page_html()
# return render(request, 'hosts.html', {'host_list': host_list, "page_html": html}) @app.route('/pager')
def pager():
li = []
for i in range(1,100):
li.append(i)
# print(li)
  
  ===================================flask的用法===============================
pager_obj = Pagination(request.args.get("page",1),len(li),request.path,request.args,per_page_count=10)
# print(request.args)
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,condition=path) if __name__ == '__main__':
app.run(debug=True)

pager.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width">
<title>Title</title>
<link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css">
<style>
.container{
margin-top: 20px;
}
</style>
</head>
<body>
<div class="container">
<a href="/add?{{ condition }}"><button class="btn btn-primary">添加</button></a>
<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>

用flask实现的分页的更多相关文章

  1. Python利用flask sqlalchemy实现分页效果

    Flask-sqlalchemy是关于flask一个针对数据库管理的.文中我们采用一个关于员工显示例子. 首先,我们创建SQLALCHEMY对像db. from flask import Flask, ...

  2. 【Flask】查询分页问题处理

    遇到两次查询结果分页的问题, 查询出结果后, 翻页时导致查询条件失效. 处理方式 1. 路由中不放page参数 写成 @testfile.route("/test-file", m ...

  3. flask应用的分页

    Flask-SQLAlchemy支持分页 https://www.jianshu.com/p/5e03cd202728

  4. [Flask]jinja2渲染分页导航部件

    注意: 1.在视图函数中通过request.args.get('page')获取page数,并将page传给macros.html模板文件 效果: 点击8,就跳转到第8页数据了 视图函数 @app.r ...

  5. Flask分页

    一.flask实现的分页组件 from urllib.parse import urlencode,quote,unquote class Pagination(object): "&quo ...

  6. Flask学习【第5篇】:用Falsk实现的分页

    Flask实现的分页组件 from urllib.parse import urlencode,quote,unquote class Pagination(object): "" ...

  7. Flask【第5篇】:用Falsk实现的分页

    用flask实现的分页 一.flask实现的分页组件 from urllib.parse import urlencode,quote,unquote class Pagination(object) ...

  8. Django自定义分页、bottle、Flask

    一.使用django实现之定义分页 1.自定义分页在django模板语言中,通过a标签实现; 2.前段a标签使用<a href="/user_list/?page=1"> ...

  9. Vue中结合Flask与Node.JS的异步加载功能实现文章的分页效果

    你好!欢迎阅读我的博文,你可以跳转到我的个人博客网站,会有更好的排版效果和功能. 此外,本篇博文为本人Pushy原创,如需转载请注明出处:http://blog.pushy.site/posts/15 ...

随机推荐

  1. scrapy和scrapy_redis入门

    Scarp框架 需求 获取网页的url 下载网页内容(Downloader下载器) 定位元素位置, 获取特定的信息(Spiders 蜘蛛) 存储信息(ItemPipeline, 一条一条从管里走) 队 ...

  2. python的基础初始第二天

    1.基础数据类型初始 1,数字类型,int,用于计算,+ ,- ,*, /,加,减,乘,除.在python2有整型和长整型之分(3344L),在python3 已经不区分了. 2,字符串类型strin ...

  3. 【译】第九篇 SQL Server安全透明数据加密

    本篇文章是SQL Server安全系列的第九篇,详细内容请参考原文. Relational databases are used in an amazing variety of applicatio ...

  4. react踩坑记录——使用fetch获取json数据报错

    报错: 原因其实是list.json文件路径错误,该文件路径是相对于index.html的,而不是App.js或者index.js.

  5. Nginx(二) nginx 无法启动

    有时候在客户端输入:nginx 但是终端会输出以下,显示启动失败 nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already i ...

  6. Object的wait/notify/notifyAll&&Thread的sleep/yield/join/holdsLock

    一.wait/notify/notifyAll都是Object类的实例方法 1.wait方法:阻塞当前线程等待notify/notifyAll方法的唤醒,或等待超时后自动唤醒. wait等待其实是对象 ...

  7. vue2.0安装

    vue2.0环境安装 参考网站http://www.open-open.com/lib/view/open1476240930270.html (以上博客vue init webpack-simple ...

  8. Django中的信号基础知识

    Django中提供了“信号调度”,用于在框架执行操作时解耦.通俗来讲,就是一些动作发生的时候,信号允许特定的发送者去提醒一些接受者. 1.Django内置信号 1 2 3 4 5 6 7 8 9 10 ...

  9. python模块-----pymysql

    一.安装 本模块为python第三方模块,需要单独安装.作用为调用mysql接口执行模块 pip3 install pyMySql 操作步骤: #!/usr/bin/python3 import py ...

  10. [转] 理解NLP中的卷积&&Pooling

    转自:http://blog.csdn.net/malefactor/article/details/51078135 CNN是目前自然语言处理中和RNN并驾齐驱的两种最常见的深度学习模型.图1展示了 ...