class Pager():
"""
baseurl: url
current_page: 目前頁碼
data_entry:每頁多少條資料
database_message:{db:'',
user:'',
passwd:'',
host:'',
}
"""
def __init__(self,current_page, baseurl, data_entry):
self.baseurl = baseurl
self.current_page = current_page
try:
self.current_page = int(self.current_page)
except:
self.current_page = 1
if self.current_page < 1:
self.current_page = 1
conn = pymysql.connect(host='123.206.96.209', port=3306, user='root', passwd='1,Aa13473860551', db='test')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 假設目前頁碼是第10頁
# current_page = 1
# 總行數
cursor.execute('select count(1) as count from opt')
total_rows = cursor.fetchone()['count']
# 總頁數
total_pages = total_rows // data_entry
# print(total_rows)
# 目前頁最大行号 + 1 (因為下面要使用小于,是以這要 + 1)
max_line = total_rows - (self.current_page - 1) * data_entry + 1
cursor.execute(
"select o.id,o.email,o.name,o.time from opt as o inner join (select id from opt where id < %s order by id desc limit %s) as f on f.id=o.id",
(max_line,data_entry))
self.current_page_data = cursor.fetchall()
# print(current_page_data)
cursor.close()
conn.close()
if total_pages < 11:
s = 1
t = total_pages + 1
else:
if self.current_page < 6:
s = 1
t = 12
else:
if self.current_page + 5 > total_pages:
s = self.current_page - 5
t = self.current_page + 1
else:
s = self.current_page - 5
t = self.current_page + 6
tag_list = []
if self.current_page == 1:
# perv = "<a href='javascript:void(0)'>上一頁</a>"
pass
else:
perv = "<a href='/%s/%s'>上一頁</a>" % (self.baseurl,(self.current_page - 1),)
home_page = "<a href='/%s/%s'>首頁</a>" % (self.baseurl,1,)
tag_list.append(perv)
tag_list.append(home_page)
for i in range(s, t):
if i == self.current_page:
s = "<a class=active href='/%s/%s'>%s</a>" % (self.baseurl,i, i)
else:
s = "<a href='/%s/%s'>%s</a>" % (self.baseurl,i, i)
tag_list.append(s)
if self.current_page == total_pages:
#end_page = "<a href='javascript:void(0)'>末頁</a>"
pass
else:
end_page = "<a href='/%s/%s'>末頁</a>" % (self.baseurl,total_pages,)
nex = "<a href='/%s/%s'>下一頁</a>" % (self.baseurl, (self.current_page + 1),)
tag_list.append(end_page)
tag_list.append(nex)
total_page = "<span>共 %s 頁</span>" % (total_pages)
go = """
<input type='text'style='width:35px;height:25px;'/>
<a href='javascript:void(0)' onclick="Jump('%s',this);">跳轉</a>
""" % ('/' + self.baseurl + '/')
jump = '''
<script>
function Jump(baseUrl, ths){
var val = ths.previousElementSibling.value;
if(val.trim().length >0){
location.href = baseUrl +val;
}
}
</script>
'''
tag_list.append(total_page)
tag_list.append(go)
tag_list.append(jump)
self.current_str_page = "".join(tag_list)
# print(current_str_page)
def returnPage(self):
return self.current_str_page, self.current_page_data
用法:
class IndexHandler(tornado.web.RequestHandler):
def get(self, page):
import time
a = time.time()
傳入 page :目前頁碼 url 每頁條目數
pager = Pager(page, 'index',10)
current_str_page,current_dic = pager.returnPage()
b = time.time()
print(b-a)
self.render('index.html', pager = current_str_page,current_dic = current_dic)