# coding:utf8 # __author: Administrator # date: 2018/3/7 0007 # /usr/bin/env python import tornado.web from math import ceil LIST_INFO =[ {‘username‘:‘ld‘,"email":‘[email protected]‘}, ] class Pagination: #传入当前页,总条数 def __init__(self,current_page,total_num): try: current_page = int(current_page) except: current_page = 1 if current_page < 1: current_page = 1 #总条数 self.total_num=total_num #默认每页显示内容 self.page_size=5 #默认分页栏显示几页 self.page_show_num=11 #当前页 self.current_page=current_page #总页数 self.total_page=ceil(total_num/self.page_size) #当前路径 self.base_url=‘/index/‘ #设置当前路径 @property def set_base_url(self,base_url): self.base_url=base_url #设置页面显示条数 @property def set_size(self,page_size): self.page_size=page_size #设置分页类显示数量 @property def set_page_show_num(self,page_show_num): self.page_show_num=page_show_num #设置总页数 @property def set_total(self,page_total): self.total_page=page_total #获取分页栏的页数范围,返回列表 def get_list(self): cur = (self.current_page - 1) * self.page_size show_page = [] mid_page=int(self.page_show_num/2) if self.total_page <= self.page_show_num: show_page = range(1, self.total_page + 1) else: if self.current_page <= mid_page: show_page = range(1, self.page_show_num+1) elif self.current_page + mid_page >= self.total_page: show_page = range(self.total_page - self.page_show_num +1, self.total_page + 1) else: show_page = range(self.current_page - mid_page, self.current_page + mid_page+1) return show_page #获取当前页,开始条数 @property def start(self): return (self.current_page - 1) * 5 #获取当前页结束条数 @property def end(self): end_page_num=0 cur_num=(self.current_page-1)*self.page_size if cur_num+self.page_size > self.total_num: end_page_num=self.total_num else: end_page_num=cur_num+self.page_size return end_page_num #获取上一页 @property def pre_page(self): page_str=‘‘ if self.current_page>1: page_str = ‘<a href="%s%s">上一页</a>‘%(self.base_url,self.current_page - 1) return page_str #获取下一页 @property def next_page(self): page_str=‘‘ if self.current_page < self.total_page: page_str = ‘<a href="%s%s">下一页</a>‘%(self.base_url,self.current_page + 1) return page_str #获取第一页 @property def first_page(self): page_str=‘‘ if self.total_page>1 and self.current_page!=1: page_str=‘<a href="%s1">首页</a>‘%self.base_url return page_str #获取尾页 @property def last_page(self): page_str=‘‘ if self.total_page>1 and self.current_page!=self.total_page: page_str = ‘<a href="%s%s">尾页</a>‘ % (self.base_url,self.total_page) return page_str #拼接html和javascript进行跳转页面 @property def go_page(self): page_str=‘‘ if self.total_page>1: inp="""<input type=‘text‘ /><a onclick="Jump(‘%s‘,this)">GO</a>"""%(self.base_url) script=""" <script> function Jump(baseUrl,ths){ var v=ths.previousElementSibling.value; if(v.trim().length>0){ location.href=baseUrl+v; } } </script> """ page_str=inp+script return page_str #返回模板页面所需要显示的html,需要在模板也进行还原, {% raw XXX %} @property def page_str(self): page_list=self.get_list() page_list_num=[] page_list_num.append(self.first_page) page_list_num.append(self.pre_page) for p in page_list: if self.current_page==p: temp="<a href=‘%s%s‘ class=‘cur‘>%s</a>"%(self.base_url,p,p) else: temp = "<a href=‘%s%s‘>%s</a>" % (self.base_url, p, p) page_list_num.append(temp) page_list_num.append(self.next_page) page_list_num.append(self.last_page) page_list_num.append(self.go_page) return ‘‘.join(page_list_num) class IndexHandler(tornado.web.RequestHandler): #使用分页类 def get(self,page): total_page = int(ceil(len(LIST_INFO) / 5)) #获取页面分页信息 Page=Pagination(page,len(LIST_INFO)) #获取数据信息 info = LIST_INFO[Page.start:Page.end] self.render(‘home/index.html‘, list_info=info, p=int(page), sp=Page.page_str) #原来分页方式,不使分页类,适用性不高 def get2(self,page): try: page = int(page) except: page=1 if page<1: page=1 #每页显示5条,page是当前页 total_page = int(ceil(len(LIST_INFO)/5)) cur=(page-1)*5 info=[] show_page=[] if cur+5 > len(LIST_INFO): info = LIST_INFO[cur:] else: info = LIST_INFO[cur:cur+5] if total_page <= 11: show_page=range(1,total_page+1) else: if page <= 5: show_page=range(1,12) elif page+5>=total_page: show_page=range(total_page-10,total_page+1) else: show_page=range(page-5,page+6) self.render(‘home/index.html‘,list_info=info,p=page,tp=total_page,sp=show_page) #接收post传递数据 def post(self, page): user = self.get_argument(‘username‘) email = self.get_argument(‘email‘) temp = {‘username‘:user,‘email‘:email} LIST_INFO.append(temp) self.redirect(‘/index/‘+page)
前端模板
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <style> .page a{ margin-left: 5px; padding: 5px; background-color: lawngreen; } .page a:hover{ background-color: rebeccapurple; } .page .cur{ background-color: gold !important; } </style> <body> <h1>提交数据</h1> <form method="post" action="/index/{{p}}"> <input name="username" type="text"/> <input name="email" type="text"/> <input type="submit" value="提交"/> </form> <h1>显示数据</h1> <table> <tr> <th>用户名</th> <th>邮箱</th> </tr> {% for line in list_info %} <tr> <td>{{line[‘username‘]}}</td> <td>{{line[‘email‘]}}</td> </tr> {% end %} </table> <div class="page"> {% raw sp %} </div> </body> </html>