首页 > 代码库 > 页面分页自定义插件

页面分页自定义插件

技术分享
#!/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=http://www.mamicode.com/{}>首页".format (self.base_url )

        if self.current_page == 1:
            temp = "<a href=‘javascript:viod(0)‘>首页</a>"
        else:
            temp = "<a href=http://www.mamicode.com/{}>首页".format( self.base_url )
        list_page.append ( temp )
        if self.current_page == 1:
            temp = "<a href=‘javascript:viod(0)‘>上一页</a>"
        else:
            temp = "<a href=http://www.mamicode.com/{}{}>上一页".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=http://www.mamicode.com/{}{}>{}".format (self.base_url ,i, i )
            else:
                temp = "<a href=http://www.mamicode.com/{}{}>{}".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=http://www.mamicode.com/{}{}>下一页".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=http://www.mamicode.com/{}{}>尾页".format( self.base_url,self.all_page() )
        list_page.append ( temp )
        jump = """<input id="paging" type =‘text‘/><a name= ‘{}‘ onclick="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 = http://www.mamicode.com/self+ret;>"""
        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

技术分享

页面分页自定义插件