Python之tornado框架实现翻页功能
1、结果如图所示,这里将html页面与网站的请求处理放在不同地方了
start.py代码
import tornado.ioloop import tornado.web from controllers import home settings = { 'static_path':'statics',#静态文件配置,需要特殊处理 'static_url_prefix':'/sss/',#标记文件开始的名字 } #路由映射,根据不同url对应到不同的类里面 application = tornado.web.Application([ (r"/index/(?P<page>\d*)", home.IndexHandler), ],**settings)#基于正则的路由,?P<page>表示为正则匹配到的内容起了一个名字 if __name__=='__main__': application.listen(8888) tornado.ioloop.IOLoop.instance().start()
home.py里面的代码
import tornado.web List_Info=[ {'username':'jay','email':'810833835@qq.com'} ] class IndexHandler(tornado.web.RequestHandler): def get(self,page): #每页显示五条数据 #page是当前页 #第一页: 0:5 List_Info[0:5] #第二页:5:10 #start: (page-1)*5 #end: (page)*5 try:page=int(page) except:page=1 print(page) if page <1: page =1 start = (page - 1)*5 end = page * 5 current_ist = List_Info[start:end] self.render('E:\\练习2\\tornado_day2\\views\\home\\index.html',list= current_ist,page=page) #E:\\练习2\\tornado_day2\\views\\home\\index.html 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)
home文件里面的index.html代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index首页</title> </head> <body> <h1>提交数据</h1> <form method="post" action="/index/{{page}}"> <input name="username" type="text"> <input name="email" type="text"> <input value="提交" type="submit"> </form> <h1>显示数据</h1> <table border="2"> <thead><tr> <th>用户名</th> <th>邮箱</th> </tr></thead> <tbody> {% for line in list %} <tr> <td>{{line['username']}}</td> <td>{{line['email']}}</td> </tr> {% end %} </tbody> </table> </body> </html>
运行结果如图
最后我们修改一下home里面的代码,实现选择页面功能
import tornado.web List_Info=[ {'username':'jay','email':'810833835@qq.com'} ] for i in range(1000): data = {'username': i, 'email': i * 2} List_Info.append(data) class IndexHandler(tornado.web.RequestHandler): def get(self,page): #每页显示五条数据 #page是当前页 #第一页: 0:5 List_Info[0:5] #第二页:5:10 #start: (page-1)*5 #end: (page)*5 try:page=int(page) except:page=1 if page <1: page =1 start = (page - 1)*5 end = page * 5#每页最多显示五条数据 current_ist = List_Info[start:end] all_page ,c = divmod(len(List_Info),5)#每五条数据一页,判断多少页 if c>0:#有余数则多一页 all_page +=1 list_page =[] print(all_page) if page <5: for p in range(9): if p+1 == page: temp = '<a class="active" href="/index/%s">%s</a>' % (p+1,p+1) else: temp = '<a href="/index/%s">%s</a>' % (p + 1, p + 1) list_page.append(temp) elif page >all_page-5:#判断页数是否在最前或者最后五页 for p in range(all_page-9,all_page): if p + 1 == page: temp = '<a class="active" href="/index/%s">%s</a>' % (p + 1, p + 1) else: temp = '<a href="/index/%s">%s</a>' % (p + 1, p + 1) list_page.append(temp) else: for p in range(page-5,page+4): if p+1 == page: temp = '<a class="active" href="/index/%s">%s</a>' % (p+1,p+1) else: temp = '<a href="/index/%s">%s</a>' % (p + 1, p + 1) list_page.append(temp) str_page = ''.join(list_page) # str_page = list_page,不写join则会在页面上显示逗号 self.render('E:\\练习2\\tornado_day2\\views\\home\\index.html',list= current_ist,page=page,str_page=str_page) #E:\\练习2\\tornado_day2\\views\\home\\index.html 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) print(List_Info)
对于index.html文件也要做少少修改
最后把home里面的代码封装一下,如图
import tornado.web List_Info=[ {'username':'jay','email':'810833835@qq.com'} ] for i in range(1000): data = {'username': i, 'email': i * 2} List_Info.append(data)#认为创建1000条数据 class Pagination: def __init__(self,current_page,all_item): all_page, c = divmod(len(all_item), 5) # 每五条数据一页,判断多少页 self.all_page=all_page if c > 0: # 有余数则多一页 all_page += 1 self.all_page = all_page#获取最大页面 try: current_page=int(current_page) except: current_page=1 if current_page <1: current_page =1 self.current_page = current_page @property def strat(self): return (self.current_page-1)*5 @property def end(self): return self.current_page * 5 def get_page(self,base_url): self.all_page, c = divmod(len(List_Info), 5) # 每五条数据一页,判断多少页 if c > 0: # 有余数则多一页 self.all_page += 1 list_page = [] if self.current_page < 5: for p in range(9): if p + 1 == self.current_page: temp = '<a class="active" href="%s %s">%s</a>' % (base_url,p + 1, p + 1) else: temp = '<a href="%s%s">%s</a>' % (base_url,p + 1, p + 1) list_page.append(temp) elif self.current_page > self.all_page - 5: # 判断页数是否在最前或者最后五页 for p in range(self.all_page - 9, self.all_page): if p == self.current_page: temp = '<a class="active" href="%s %s">%s</a>' % (base_url, p , p ) else: temp = '<a href="%s%s">%s</a>' % (base_url, p, p ) list_page.append(temp) else: for p in range(self.current_page - 5, self.current_page + 4): if p == self.current_page: temp = '<a class="active" href="%s %s">%s</a>' % (base_url, p , p) else: temp = '<a href="%s%s">%s</a>' % (base_url, p , p ) list_page.append(temp) str_page = ''.join(list_page) return str_page # str_page = list_page,不写join则会在页面上显示逗号 class IndexHandler(tornado.web.RequestHandler): def get(self,page): page_obj =Pagination(page,List_Info)#把当前访问页以及要传给前端的数据传给类里面 current_ist =List_Info[page_obj.strat:page_obj.end]#以访问字段的方式访问方法,获取数据 str_page = page_obj.get_page('/index/')#底下的页数码 self.render('E:\\练习2\\tornado_day2\\views\\home\\index.html',list= current_ist,page=page_obj.current_page,str_page=str_page) #page_obj.current_page表示把处理过的页数传进去 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) print(List_Info)
运行如图