web框架--tornado自定义分页
1、tornado_main.py
#!/usr/bin/env python # -*- coding: utf-8 -*- 6 import tornado.web import tornado.ioloop LIST_INFO = [ {‘username‘: ‘yusheng‘, ‘email‘: ‘‘} ] for i in range(200): temp = {‘username‘: str(i) + "lys", ‘email‘: str(i) + "@163.com"} LIST_INFO.append(temp) class Pagenation: def __init__(self, current_page, all_item, base_url): #当前页 内容总数 目录 try: page = int(current_page) except: page = 1 if page < 1: page = 1 all_page, c = divmod(all_item, 5) if c > 0: all_page += 1 self.current_page = page self.all_page = all_page self.base_url = base_url @property def start(self): return (self.current_page - 1) * 5 @property def end(self): return self.current_page * 5 def string_pager(self): list_page = [] if self.all_page < 11: s = 1 t = self.all_page + 1 else: if self.current_page < 6: s = 1 t = 12 else: if (self.current_page + 5) < self.all_page: s = self.current_page-5 t = self.current_page + 6 else: s = self.all_page - 11 t = self.all_page +1 first = ‘<a href = "/index/1">首页</a>‘ list_page.append(first) # 当前页 if self.current_page == 1: prev = ‘<a href = "javascript:void(0):">上一页</a>‘ else: prev = ‘<a href = "/index/%s">上一页</a>‘%(self.current_page-1,) list_page.append(prev) #页码 for p in range(s, t): if p== self.current_page: temp = ‘<a class = "active" href = "/index/%s">%s</a>‘ % (p, p) else: temp = ‘<a href = "/index/%s">%s</a>‘ % (p, p) list_page.append(temp) # 尾页 if self.current_page == self.all_page: nex = ‘<a href = "javascript:void(0):">下一页</a>‘ else: nex = ‘<a href = "/index/%s">下一页</a>‘ % (self.current_page + 1,) list_page.append(nex) last = ‘<a href = "/index/%s">尾页</a>‘ % (self.all_page) list_page.append(last) #跳转 jump = ‘‘‘<input type="text"><a onclick = "Jump(‘%s‘,this);">GO</a>‘‘‘ % (‘/index/‘) script = ‘‘‘ <script> function Jump(baseUrl,ths){ var val = ths.previousElementSibling.value; if (val.trim().length > 0){ location.href = baseUrl + val; } } </script> ‘‘‘ list_page.append(jump) list_page.append(script) str_page = "".join(list_page) return str_page class IndexHandler(tornado.web.RequestHandler): def get(self, page): obj = Pagenation(page, len(LIST_INFO), ‘/index/‘) current_list = LIST_INFO[obj.start:obj.end] str_page = obj.string_pager() self.render(‘index.html‘, list_info=current_list, current_page=obj.current_page, str_page=str_page) application = tornado.web.Application([ (r‘/index/(?P<page>\d*)‘, IndexHandler) ]) if __name__ == "__main__": application.listen(8080) tornado.ioloop.IOLoop.instance().start()
2、index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .pager a{ display: inline-block; padding: 5px 6px; margin: 10px 3px; border: 1px solid #2b669a; text-decoration:none; } .pager a.active{ background-color: #2b669a; color: white; } </style> </head> <body> <h3>显示数据</h3> <table border="1"> <thead> <tr> <th>用户名</th> <th>邮箱</th> </tr> </thead> <tbody> {% for line in list_info %} <tr> <td>{{line[‘username‘]}}</td> <td>{{line[‘email‘]}}</td> </tr> {% end %} </tbody> </table> <div class="pager"> {% raw str_page %} </div> </body> </html>
3、图示
相关推荐
selectY 2020-07-18
zhangxuelong 2020-06-14
zhangxuelong 2020-06-14
牧码人 2020-06-14
hjhmpl 2020-06-14
thundor 2020-05-05
Cagey 2020-04-25
KarlDoenitz 2020-04-16
牧码人 2020-01-25
KarlDoenitz 2019-12-28
hjhmpl 2019-12-25
selectY 2019-12-11
KarlDoenitz 2019-12-06
selectY 2019-12-05
Cagey 2019-12-05
hjhmpl 2019-11-03
牧码人 2019-11-03
chenzhanhai 2019-04-09
chenzhanhai 2019-09-09