一、flask实现的分页组件
__author__ = 'mrs.wc'
__filename__ = 'page_tools'
__datetime__ = '2018/3/1 21:39'
from urllib import urlencode, quote, unquote
class Pagination(object):
"""
自定义分页
"""
def __init__(self, current_page, total_count, base_url, params, per_page_count=10, max_pager_count=5):
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
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
self.max_pager_count = max_pager_count
self.half_max_pager_count = int((max_pager_count - 1) / 2)
self.base_url = base_url
import copy
params = copy.deepcopy(params)
get_dict = params.to_dict()
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):
if self.max_page_num <= self.max_pager_count:
pager_start = 1
pager_end = self.max_page_num
else:
if self.current_page <= self.half_max_pager_count:
pager_start = 1
pager_end = self.max_pager_count
else:
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
else:
pager_start = self.current_page - self.half_max_pager_count
pager_end = self.current_page + self.half_max_pager_count
page_html_list = []
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)
二、使用组件
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,condition=path)
if __name__ == '__main__':
app.run(debug=True)
三、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">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>表头1