分页

class Pager():
    """
    baseurl: url
    current_page: 当前页码
    data_entry:每页多少条数据
    database_message:{db:'',
    user:'',
    passwd:'',
    host:'',
    }
    """
    def __init__(self,current_page, baseurl, data_entry):
        self.baseurl = baseurl
        self.current_page = current_page
        try:
            self.current_page = int(self.current_page)
        except:
            self.current_page = 1
        if self.current_page < 1:
            self.current_page = 1

        conn = pymysql.connect(host='123.206.96.209', port=3306, user='root', passwd='1,Aa13473860551', db='test')
        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
        # 假设当前页码是第10页
        # current_page = 1
        # 总行数
        cursor.execute('select count(1) as count from opt')
        total_rows = cursor.fetchone()['count']

        # 总页数
        total_pages = total_rows // data_entry
        # print(total_rows)

        # 当前页最大行号 + 1 (因为下面要使用小于,所以这要 + 1)
        max_line = total_rows - (self.current_page - 1) * data_entry + 1

        cursor.execute(
            "select o.id,o.email,o.name,o.time from opt as o inner join (select id from opt where id < %s order by id desc limit %s) as f on f.id=o.id",
            (max_line,data_entry))
        self.current_page_data = cursor.fetchall()
        # print(current_page_data)
        cursor.close()
        conn.close()

        if total_pages < 11:
            s = 1
            t = total_pages + 1
        else:
            if self.current_page < 6:
                s = 1
                t = 12
            else:
                if self.current_page + 5 > total_pages:
                    s = self.current_page - 5
                    t = self.current_page + 1
                else:
                    s = self.current_page - 5
                    t = self.current_page + 6

        tag_list = []
        if self.current_page == 1:
            # perv = "<a href='javascript:void(0)'>上一页</a>"
            pass
        else:
            perv = "<a href='/%s/%s'>上一页</a>" % (self.baseurl,(self.current_page - 1),)
            home_page = "<a href='/%s/%s'>首页</a>" % (self.baseurl,1,)
            tag_list.append(perv)
            tag_list.append(home_page)

        for i in range(s, t):
            if i == self.current_page:
                s = "<a class=active href='/%s/%s'>%s</a>" % (self.baseurl,i, i)
            else:
                s = "<a href='/%s/%s'>%s</a>" % (self.baseurl,i, i)
            tag_list.append(s)

        if self.current_page == total_pages:
             #end_page = "<a href='javascript:void(0)'>末页</a>"
            pass
        else:
            end_page = "<a href='/%s/%s'>末页</a>" % (self.baseurl,total_pages,)
            nex = "<a href='/%s/%s'>下一页</a>" % (self.baseurl, (self.current_page + 1),)
            tag_list.append(end_page)
            tag_list.append(nex)

        total_page = "<span>共 %s 页</span>" % (total_pages)

        go = """
                <input type='text'style='width:35px;height:25px;'/>
                <a href='javascript:void(0)' onclick="Jump('%s',this);">跳转</a>
            """ % ('/' + self.baseurl + '/')
        jump = '''
            <script>
                function Jump(baseUrl, ths){
                    var val = ths.previousElementSibling.value;
                    if(val.trim().length >0){
                        location.href = baseUrl +val;
                    }
                }
            </script>
            '''

        tag_list.append(total_page)
        tag_list.append(go)
        tag_list.append(jump)
        self.current_str_page = "".join(tag_list)

        # print(current_str_page)
    def returnPage(self):
        return self.current_str_page, self.current_page_data

  

用法:

class IndexHandler(tornado.web.RequestHandler):
def get(self, page):
import time
a = time.time()
      传入 page :当前页码 url 每页条目数
pager = Pager(page, 'index',10)
current_str_page,current_dic = pager.returnPage()
b = time.time()
print(b-a)
self.render('index.html', pager = current_str_page,current_dic = current_dic)
posted @ 2016-08-09 18:00  Echo/  阅读(125)  评论(0编辑  收藏  举报