web框架--tornado自定义分页

1、tornado_main.py

  1 #!/usr/bin/env python
  2 # -*- coding: utf-8 -*-
  3 
  4 
  5
  6 
  7 import tornado.web
  8 import tornado.ioloop
  9 
 10 LIST_INFO = [
 11     {'username': 'yusheng', 'email': '[email protected]'}
 12 ]
 13 for i in range(200):
 14     temp = {'username': str(i) + "lys", 'email': str(i) + "@163.com"}
 15     LIST_INFO.append(temp)
 16 
 17 
 18 class Pagenation:
 19 
 20     def __init__(self, current_page, all_item, base_url):  #当前页 内容总数 目录
 21         try:
 22             page = int(current_page)
 23         except:
 24             page = 1
 25         if page < 1:
 26             page = 1
 27 
 28         all_page, c = divmod(all_item, 5)
 29         if c > 0:
 30             all_page += 1
 31 
 32         self.current_page = page
 33         self.all_page = all_page
 34         self.base_url = base_url
 35 
 36     @property
 37     def start(self):
 38         return (self.current_page - 1) * 5
 39 
 40     @property
 41     def end(self):
 42         return self.current_page * 5
 43 
 44     def string_pager(self):
 45         list_page = []
 46         if self.all_page < 11:
 47             s = 1
 48             t = self.all_page + 1
 49         else:
 50             if self.current_page < 6:
 51                 s = 1
 52                 t = 12
 53             else:
 54                 if (self.current_page + 5) < self.all_page:
 55                     s = self.current_page-5
 56                     t = self.current_page + 6
 57                 else:
 58                     s = self.all_page - 11
 59                     t = self.all_page +1
 60 
 61         first = '首页'
 62         list_page.append(first)
 63         # 当前页
 64         if self.current_page == 1:
 65             prev = '上一页'
 66         else:
 67             prev = '上一页'%(self.current_page-1,)
 68         list_page.append(prev)
 69 
 70         #页码
 71         for p in range(s, t):
 72             if p== self.current_page:
 73                 temp = '%s' % (p, p)
 74             else:
 75                 temp = '%s' % (p, p)
 76             list_page.append(temp)
 77 
 78 
 79 
 80         # 尾页
 81         if self.current_page == self.all_page:
 82             nex = '下一页'
 83         else:
 84             nex = '下一页' % (self.current_page + 1,)
 85         list_page.append(nex)
 86 
 87         last = '尾页' % (self.all_page)
 88         list_page.append(last)
 89 
 90 
 91         #跳转
 92         jump = '''GO''' % ('/index/')
 93         script = '''
 94             
102         '''
103         list_page.append(jump)
104         list_page.append(script)
105         str_page = "".join(list_page)
106 
107         return str_page
108 
109 class IndexHandler(tornado.web.RequestHandler):
110 
111     def get(self, page):
112         obj = Pagenation(page, len(LIST_INFO), '/index/')
113         current_list = LIST_INFO[obj.start:obj.end]
114         str_page = obj.string_pager()
115         self.render('index.html', list_info=current_list, current_page=obj.current_page, str_page=str_page)
116 
117 application = tornado.web.Application([
118     (r'/index/(?P\d*)', IndexHandler)
119 
120 ])
121 
122 
123 if __name__ == "__main__":
124     application.listen(8080)
125     tornado.ioloop.IOLoop.instance().start()

2、index.html

 1 
 2 "en">
 3 
 4     "UTF-8">
 5     Title
 6     
20 
21 
22     

显示数据

23 "1"> 2425262728293031 {% for line in list_info %} 3233343536 {% end %} 3738
用户名 邮箱
{{line['username']}} {{line['email']}}
39
class="pager"> 40 {% raw str_page %} 41
42 43

3、图示

web框架--tornado自定义分页_第1张图片

你可能感兴趣的:(web框架--tornado自定义分页)