Python Tornado get请求的分页(模板渲染)

Pagination , 加一个search-filter,提升兼容性,urlencode路径拼接

import math
from urllib import urlencode
#Python3   from urllib.parse import urlencode


class Pagination:
    def __init__(self, search_filter, all_item, limit, show_page):
        all_pager, c = divmod(len(all_item), limit)
        if c > 0:
            all_pager += 1
        try:
            current_page = int(search_filter['page'])
        except Exception as e:
            current_page = 1
        if current_page <= 0:
            current_page = 1

        self.items = all_item
        self.current_page = current_page
        self.all_pager = all_pager
        self.show_page = show_page
        self.limit = limit
        self.search = search_filter

    @property
    def start(self):
        return (self.current_page - 1) * self.limit

    @property
    def end(self):
        return self.current_page * self.limit

    def show_pagination(self, baseurl):
        list_page = []

        # 上一页
        last_page = '上一页' % (baseurl, urlencode(self.last_page()))
        list_page.append(last_page)
        if self.all_pager < self.show_page:
            s = 1
            t = self.all_pager
        else:
            if self.current_page <= int(math.ceil(self.show_page / 2.0)):
                s = 1
                t = self.show_page
            elif self.current_page > int(math.ceil(self.show_page / 2.0)):
                if self.current_page + self.show_page / 2 > self.all_pager:
                    s = self.all_pager - self.show_page
                    t = self.all_pager
                else:
                    s = self.current_page - self.show_page / 2
                    t = self.current_page + self.show_page / 2

        for p in range(s, t + 1):
            if p == self.current_page:
                tmp = '%s' % (baseurl, urlencode(self.search), p)
            else:
                self.search['page'] = p
                tmp = '%s' % (baseurl, urlencode(self.search), p)
            list_page.append(tmp)

        # 下一页
        next_page = '下一页' % (baseurl, urlencode(self.next_page()))
        list_page.append(next_page)

        # 跳转到
        jump = """
        GO""" % (baseurl,)
        script = """
                """% (urlencode(self.search))
        list_page.append(jump)
        list_page.append(script)
        return "".join(list_page)

    def current_list_info(self):
        current_list = self.items[self.start:self.end]
        return current_list

    def last_page(self):
        if self.current_page > 1:
            self.search['page'] = self.current_page - 1
        else:
            self.search['page'] = 1
        return self.search

    def next_page(self):
        if self.current_page < self.all_pager:
            self.search['page'] = self.current_page + 1
        else:
            self.search['page'] = self.all_pager
        return self.search

handler

class IndexHandler(AppBaseHandler):
    def get(self, *args, **kwargs):
        # self.render('index.html')
        try:
            params = Schema({
                Optional("page", default=1): Use(int),
                Optional("limit", default=10): Use(int),
                Optional("sort", default='id'): Use(str),
            }).validate(self.get_params())
        except SchemaError as e:
            return self.response_help(statuscode="401", status_msg=e)
        page = params['page']
        limit = params['limit']
        all_items = [{'id': i + 1, "name": "maotai", "email": "123456"} for i in range(300)]
        page_obj = Pagination({'page':page}, all_items, limit, 5)
        current_list = page_obj.current_list_info()
        str_page = page_obj.show_pagination("")
        self.render("Test.html", list_info=current_list, current_page=page, str_page=str_page)

html




    
    Test
    



    {% for line in list_info %}
    
        {% block success %}
        
        {% end %}
    
    {% end %}
    
id 名称 简介
{{ line['id'] }} {{ line['name'] }} {{ line['email'] }}
{% raw str_page %}

原文链接:https://blog.csdn.net/kukudehui/rss/list

你可能感兴趣的:(Python Tornado get请求的分页(模板渲染))