python custom finisher () Transfer https://www.cnblogs.com/yuanchenqi/articles/7652353.html

"" " 
Paging component uses examples: 

    obj = Pagination (request.GET.get ( 'Page',. 1), len (USER_LIST), request.path_info) 
    page_user_list = USER_LIST [obj.start: obj.end] 
    page_html = obj. page_html () 

    return the render (Request, 'index.html', { 'Users': page_user_list, 'page_html': page_html}) 


"" " 


class Pagination (Object): 

    DEF  the __init__ (Self, current_page, ALL_COUNT are, the base_url, per_page_num = 2, pager_count =. 11 ):
         "" " 
        package tab data 
        : param current_page: this page 
        : param all_count: total number of pieces of data in the database 
        : param per_page_num: per bar shows the number of data 
        : param base_url:URL prefix shown tab 
        number of the page shows up:: param pager_count
        """

        try:
            current_page = int(current_page)
        except Exception as e:
            current_page = 1

        if current_page <1:
            current_page = 1

        self.current_page = current_page

        self.all_count = all_count
        self.per_page_num = per_page_num

        self.base_url = base_url

        # 总页码
        all_pager, tmp = divmod(all_count, per_page_num)
        if tmp:
            all_pager += 1
        self.all_pager = all_pager


        self.pager_count = pager_count
        self.pager_count_half = int((pager_count - 1) / 2)

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

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

    def page_html(self):
        # 如果总页码 < 11个:
        if self.all_pager <= self.pager_count:
            pager_start = 1
            pager_end Self.all_pager +. 1 =
         # Total p>. 11 
        the else :
             # this page if <= 11/2 up a page displayed on a page 
            IF self.current_page <= self.pager_count_half: 
                pager_start =. 1 
                pager_end = self.pager_count. 1 + # Current p is greater than. 5 the else :
                 # p final turn IF (self.current_page self.pager_count_half +)> self.all_pager: 
                    pager_end = self.all_pager +. 1 
                    pager_start = self.all_pager - +. 1 self.pager_count
                 the else :

            
            
                
                    pager_start = self.current_page - self.pager_count_half
                    pager_end = self.current_page + self.pager_count_half + 1

        page_html_list = []

        first_page = '<li><a href="%s?page=%s">首页</a></li>' % (self.base_url,1,)
        page_html_list.append(first_page)

        if self.current_page <= 1:
            prev_page = '<li class="disabled"><a href="#">上一页</a></li>'
        else:
            prev_page = '<li><a href="%s?page=%s">上一页</a></li>' % (self.base_url,self.current_page - 1,)

        page_html_list.append(prev_page)

        for i in range(pager_start, pager_end):
            if i == self.current_page:
                temp = '<li class="active"><a href="%s?page=%s">%s</a></li>' % (self.base_url,i, i,)
            else:
                temp = '<li><a href="%s?page=%s">%s</a></li>' % (self.base_url,i, i,)
            page_html_list.append(temp)

        if self.current_page >= self.all_pager:
            next_page = '<li class="disabled"><a href="#">下一页</a></li>'
        else:
            next_page = '<li><a href="%s?page=%s">下一页</a></li>' % (self.base_url,self.current_page + 1,)
        page_html_list.append(next_page)

        last_page = '<li><a href="%s?page=%s">尾页</a></li>' % (self.base_url,self.all_pager,)
        page_html_list.append(last_page)

        return ''.join(page_html_list)

 

Guess you like

Origin www.cnblogs.com/wangdamao/p/11568905.html
Recommended