1. 程式人生 > >分頁設計

分頁設計

import cad 提交數據 charset tab dir ews 映射 val

#!/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>

 

分頁設計