tornado框架2.1_分页功能实现

 1 """
 2 分页下标的设计
 3 1.取得总页数(列表消息长度/每页显示的消息条数)
 4 2.如果余数大于0,总页数加一
 5 3.通过循环总页码来生成页码的A标签的html代码
 6 4.用join来把3中生成的a标签连接起来,然后在render中传给index.html
 7 5.在index.html中接收str_page时要用{% raw str_page %}这种格式
 8 因为如果用{{str_page}}时,tornado为了安全会对html代码添加转义符让他们表示
 9 原义而不是html代码的意思,所以为了传过去时能执行,应该用那种格式去掉tornado的转义
10 6.实现实现当前页前五页和后五页
11     当 当前页<11时:
12         s=1,t=总页数
13     当 当前页>11时:
14         如果 当前页<=6:
15             s=1,t=11
16         如果 当前页>6:
17             如果 当前页+5 >总页数:
18                 s=总页数-10,t=总页数
19         else:
20             s=当前页-5,当前页+5
21 
22 """
  • 其余脚本跟tornado_2一致
 1 import tornado.web
 2 
 3 LIST_INFO = [
 4     {'username':'xiaobobo','email':'xiao@qq.com'},
 5 ]
 6 
 7 for i in range(99):
 8     temp = {'username':'jialing'+str(i),'email':str(i)+'333@ww'}
 9     LIST_INFO.append(temp)
10 
11 class IndexHandler(tornado.web.RequestHandler):
12     def get(self, page):
13         # print(mid,num)
14         # 每页显示五条数据,page是当前页
15         # 第一页:0:5
16         # LIST_INFO[0:5]
17         # 第二页:5:10
18         # LIST_INFO[5:10]
19         # start : (page-1)*5
20         #  end  :page*5
21         try:
22             # 当page为空,或者数字字符串混合时都会发生转换异常,执行except
23             page = int(page)  # page 是字符串类型
24         except:
25             page = 1
26         if page < 1:
27             page = 1
28         start = (page-1)*5
29         end = page*5
30         current_list = LIST_INFO[start:end]
31 
32         # 取得总页数
33         # divmod(x,y):取得得数all_pager和余数c
34         all_pager ,c = divmod(len(LIST_INFO),5)
35         if c > 0:
36             all_pager += 1
37         # 用来存储页码a标签的html代码
38         list_page = []
39         if all_pager<11:
40             s = 1
41             t = all_pager
42         else:
43             if page<=6:
44                 s = 1
45                 t = 11
46             else:
47                 if( page+5)>all_pager:
48                     s = all_pager-10
49                     t = all_pager
50                 else:
51                     s = page-5
52                     t = page+5
53         for p in range(s,t+1 ):
54             if p == page:
55                 temp = '<a class="currtentP" href="/index/%s">%s</a>' % (p , p )
56             else:
57                 temp = '<a href="/index/%s">%s</a>'%(p,p)
58             list_page.append(temp)
59         # 用join来把list_page 里的a标签连接起来(为了去掉逗号)
60         str_page = ''.join(list_page)
61 
62         self.render('home/index.html',list_info =current_list,current_page = page,str_page = str_page)
63 
64     def post(self, page):     # index.html提交后把current_page 传给page
65         user = self.get_argument('username')
66         email = self.get_argument('email')
67         temp = {'username':user,'email':email}
68         LIST_INFO.append(temp)
69         self.redirect('/index/'+page)
home.py
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6     <style>
 7         .pager a{
 8             display:inline-block;
 9             padding:5px;
10             margin:3px;
11             background-color: #4ab3ff;
12         }
13         .pager a.currtentP{
14             background-color: chartreuse;
15         }
16     </style>
17 </head>
18 <body>
19     <h1>提交数据</h1>
20     <form action="/index/{{current_page}}" method="post">
21         <input type="text" name="username">
22         <input type="text" name="email">
23         <input type="submit" value="提交">
24     </form>
25     <h1>显示数据</h1>
26     <table border="1">
27         <thead>
28             <tr>
29                 <th>用户名</th>
30                 <th>邮箱</th>
31             </tr>
32         </thead>
33         <tbody>
34             {% for line in list_info %}
35                 <tr>
36                     <td>{{line['username']}}</td>
37                     <td>{{line['email']}}</td>
38                 </tr>
39             {% end %}
40         </tbody>
41     </table>
42     <div class="pager">
43         <!--这种格式是让传过来的a标签html代码没有被tornado转义,以源码显示-->
44         {% raw str_page %}
45     </div>
46 </body>
47 </html>
index.html
  •  类化的home.py
home.py
  1 # -*- coding utf-8 -*-
  2 # coding=utf-8
  3 
  4 import tornado.web
  5 
  6 """
  7 模块化
  8 """
  9 
 10 LIST_INFO = [
 11     {'username':'xiaobobo','email':'xiao@qq.com'},
 12 ]
 13 
 14 for i in range(99):
 15     temp = {'username':'jialing'+str(i),'email':str(i)+'333@ww'}
 16     LIST_INFO.append(temp)
 17 
 18 class Pagination:
 19     def __init__(self,current_page,all_item):
 20         all_page, c = divmod(all_item, 5)
 21         if c > 0:
 22             all_page += 1
 23         try:
 24             current_page = int(current_page)
 25         except:
 26             current_page = 1
 27         if current_page < 1:
 28             current_page= 1
 29 
 30         self.current_page = current_page
 31         self.all_page = all_page
 32     @property   #以访问字段的方法去访问方法,即访问方法时不用括号后缀
 33     def start(self):
 34         return (self.current_page-1)*5
 35     @property
 36     def end(self):
 37         return self.current_page*5
 38     def page_str(self,base_url):
 39             # 用来存储页码a标签的html代码
 40         list_page = []
 41         if self.all_page < 11:
 42             s = 1
 43             t = self.all_page
 44         else:
 45             if self.current_page <= 6:
 46                 s = 1
 47                 t = 11
 48             else:
 49                 if (self.current_page + 5) >self.all_page:
 50                     s = self.all_page - 10
 51                     t = self.all_page
 52                 else:
 53                     s = self.current_page - 5
 54                     t = self.current_page + 5
 55         # 首页
 56         first_page =  '<a  href="%s1">首页</a>' % (base_url,)
 57         list_page.append(first_page)
 58 
 59          # 上一页
 60         if self.current_page ==1:
 61             # javascript:void(0):什么都不做
 62             prev_page = '<a  href="javascript:void(0);">上一页</a>'
 63         else:
 64             prev_page = '<a  href="%s%s">上一页</a>' % (base_url,self.current_page-1)
 65         list_page.append( prev_page)
 66 
 67         # 中间的页码
 68         for p in range(s, t + 1):
 69              if p == self.current_page:
 70                  temp = '<a class="currtentP" href="%s%s">%s</a>' % (base_url,p, p)
 71              else:
 72                  temp = '<a href="%s%s">%s</a>' % (base_url,p, p)
 73              list_page.append(temp)
 74 
 75         # 下一页
 76         if self.current_page == self.all_page:
 77             next_page = '<a  href="javascript:void(0);">下一页</a>'
 78         else:
 79             next_page = '<a  href="%s%s">下一页</a>' % (base_url, self.current_page + 1)
 80         list_page.append(next_page)
 81 
 82         # 尾页
 83         last_page = '<a  href="%s%s">尾页</a>' % (base_url,self.all_page)
 84         list_page.append(last_page)
 85 
 86         # 页面跳转
 87         jump = """<input type='text' /><a onclick="Jump('%s',this);">GO</a>"""%(base_url,)
 88         script = """<script>
 89             function Jump(baseUrl,ths){
 90                 var val = ths.previousElementSibling.value;
 91                 if(val.trim().length>0){
 92                     location.href = baseUrl+val;
 93                 }
 94             }
 95             </script>
 96         """
 97         list_page.append(jump)
 98         list_page.append(script)
 99 
100         str_page = ''.join(list_page)
101         return str_page
102 class IndexHandler(tornado.web.RequestHandler):
103     def get(self, page):
104         # 需要:当前页,总页数
105         #  输出:总页数
106         page_obj = Pagination(page,len(LIST_INFO))
107         # 当前页显示的元组内容范围
108         current_list = LIST_INFO[page_obj.start:page_obj.end]
109 
110         str_page = page_obj.page_str('/index/')
111         self.render('home/index.html',list_info =current_list,current_page = page_obj.current_page,str_page = str_page)
112 
113     def post(self, page):     # index.html提交后把current_page 传给page
114         user = self.get_argument('username')
115         email = self.get_argument('email')
116         temp = {'username':user,'email':email}
117         LIST_INFO.append(temp)
118         self.redirect('/index/'+page)

 

posted @ 2017-05-26 00:48  yangyongbo  阅读(290)  评论(0)    收藏  举报