分页设计

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import tornado.web
from controllers import home

setting = {
    "template_path":"views",# 模板路径配置
     "static_path":"statics", # 静态文件配置# 静态文件配置
    #"static_url_prefix":"/ss/",# 静态文件前缀
}

#路由映射  路由系统
application = tornado.web.Application([
    (r"/index/(?P<page>\d*)", home.IndexHandler),
],**setting)
#  二级匹配
application.add_handlers("buy.cai.com$",[
    (r"/index/(?P<page>\d*)", buy.IndexHandler),
])

if __name__ == "__main__":
    application.listen(1111)
    tornado.ioloop.IOLoop.instance().start()
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import tornado.web
LIST =[
    {"name":"caidapeng","pwd":'123456'},
]
for i in range(300):
    temp ={"name":"cai"+str(i),"pwd":"11aa"}
    LIST.append(temp)
class IndexHandler(tornado.web.RequestHandler):
    def get(self,page):
        # 每页显示五条 page 当前页
        # 第一页  0:5  LIST[0,5]  2 LIST[5,10]
        try:
            page = int(page)
        except:
            page = 1
        if page < 1 :
            page = 1
        start = (page-1) * 5
        end = page * 5
        current_list = LIST[start:end]

        # 总页数
        all_page,c = divmod(len(LIST),5)
        if c >0:
            all_page += 1

        # 页面显示五个页码
        print(all_page,c)
        if  page < 3 :
            if page < 0:
                st = 0
                en = 5
            else:
                st = 0
                en = 5

        elif page >all_page-3:
            st = all_page -5
            en = all_page
        else:
            st = page-3
            en = page+2
        list_page = []
        for p in range(st,en):#  参数为动态的
            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)
        self.render("home/index.html",list = current_list,currpage = page,str_page=str_page)

    def post(self, page):
        name = self.get_argument("name")
        pwd = self.get_argument("pwd")
        temp = {'name' : name , 'pwd' : pwd}
        LIST.append(temp)
        self.redirect("/index/"+page)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .pager a {
            display:inline-block;
            padding:5px;
            margin:3px;
            background-color:cadetblue;

        }
        .pager a.active{
            background-color:brown;
            color:white;

        }
    </style>
</head>
<body>
<h2>提交数据</h2>
<form method="post" action="/index/{{currpage}}">
    <input name ="name" type="text">
    <input name ="pwd" type="text">
    <input  type="submit" value="提交">
</form>
<h3>显示数据</h3>
<table>
    <tr>
          <th>姓名</th>
          <th>密码</th>
    </tr>
    {% for line in list %}
    <tr>

        <td>{{line['name']}}</td>
        <td>{{line['pwd']}}</td>

    </tr>
     {% end %}

</table>
<div class="pager">{% raw str_page %}</div>
</body>
</html>

 

 
posted @ 2017-12-04 15:42  少数派&蔡先生  阅读(220)  评论(0编辑  收藏  举报