页面分页自定义插件

#!/usr/bin/env python
import tornado.ioloop
import tornado.web
from controllers import home
from controllers import account

settings ={
    'template_path':'views',#html文件模板路径配置
    'static_path':'static',#css,js文件路径配置
    # 'static_url_prefix':'/sss/',
}

application = tornado.web.Application([
        (r"/index/(?P<page>\d*)", home.IndexHandler),
    # (r"/index", home.IndexHandler),
    #     (r"/login",account.LoginHandler),
    #     (r"/register",account.RegisterHandler),
        ],**settings)
if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()
start.py
#!/usr/bin/env python
import tornado.web
LIST_INFO = [
    {'username':'taochen','email':'540123685@qq.com'}
]
for i in range(99):
    temp = {'username':'qwee'+str(i),"email":str(i)+"ewq"}
    LIST_INFO.append(temp)

class Paging:
    def __init__(self,base_url,current_page,len_info,page_info):

        try:
            current_page = int(current_page)
        except:
            current_page = 1
        if current_page < 1:
            current_page = 1
        self.current_page = current_page
        self.len_info = len_info
        self.page_info =page_info
        self.base_url = "{}{}{}".format('/',base_url,'/')
    def all_page(self):
        all_pager, c = divmod (self.len_info, self.page_info )
        if c > 0:
            all_pager += 1
        return all_pager
    def start_page(self):
        return (self.current_page - 1)*self.page_info

    def end_page(self):
        return (self.current_page)*self.page_info


    def paging(self):
        list_page = []

        if self.all_page() < 11:
            s = 1
            t = self.all_page()
        else:
            if self.current_page < 6:
                s = 1
                t = 11
            else:
                if (self.current_page + 5) > self.all_page():
                    s = self.all_page() - 10
                    t = self.all_page()
                else:
                    s = self.current_page - 5
                    t = self.current_page + 5
        temp = "<a  href={}>首页</a>".format (self.base_url )

        if self.current_page == 1:
            temp = "<a href='javascript:viod(0)'>首页</a>"
        else:
            temp = "<a href={}>首页</a>".format( self.base_url )
        list_page.append ( temp )
        if self.current_page == 1:
            temp = "<a href='javascript:viod(0)'>上一页</a>"
        else:
            temp = "<a href={}{}>上一页</a>".format( self.base_url,self.current_page - 1 )
        list_page.append ( temp )
        for i in range ( s, t + 1 ):
            if i == self.current_page:
                temp = "<a class='active' href={}{}>{}</a>".format (self.base_url ,i, i )
            else:
                temp = "<a href={}{}>{}</a>".format (self.base_url, i, i )
            list_page.append ( temp )

        if self.current_page >= self.all_page():
            temp = "<a href='javascript:viod(0)'>下一页</a>"
        else:
            temp = "<a href={}{}>下一页</a>".format( self.base_url,self.current_page + 1 )
        list_page.append ( temp )
        if self.current_page == self.all_page():
            temp = "<a href='javascript:viod(0)'>尾页</a>"
        else:
            temp = "<a href={}{}>尾页</a>".format( self.base_url,self.all_page() )
        list_page.append ( temp )
        jump = """<input id="paging" type ='text'/><a name= '{}' οnclick="Jump('{}',this)">Go</a>""".format(self.all_page(),self.base_url)
        script = """
            <script>
                function Jump(self,ths) {
                    var ret = ths.previousElementSibling.value;
                    if(ret.trim().length > 0){
                        if( ret > parseInt(ths.name)){
                            ret = parseInt(ths.name);
                        }
                    location.href = self+ret;
                    }
                }

            </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):

        current_obj = Paging("index",page,len(LIST_INFO),5)
        current_list = LIST_INFO[current_obj.start_page():current_obj.end_page()]
        self.render('home/index.html',list_info = current_list,current_page = page, str_page = current_obj.paging())
    def post(self, page):
        user = self.get_argument("username")
        email =self.get_argument("email")
        temp = {'username':user,'email':email}
        LIST_INFO.append(temp)
        self.redirect('/index/'+page)
home.py
<!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: green;
            color: white;
        }
        .pager a.active{
            background-color: hotpink;
        }

    </style>
</head>
<body>
     <h1>提交数据</h1>
    <form method="post" action="/index/{{current_page}}">
        <input name="username" type="text">
        <input name="email" type="text">
        <input type="submit" value="提交">
    </form>
    <h1>显示数据</h1>
    <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>
index

转载于:https://www.cnblogs.com/shiluoliming/p/6556527.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值