Django pager application

First save as a file

class Pagination(object):
    def __init__(self, current_page, all_count, per_page_num=2, pager_count=11):
        """
        Paging data package
        : Param current_page: Current Page
        : Param all_count: Total number of pieces of data in the database
        : Param per_page_num: the number of data displayed per page
        Up display page number:: param pager_count

        usage:
        queryset = model.objects.all()
        page_obj = Pagination(current_page,all_count)
        page_data = queryset[page_obj.start:page_obj.end]
        Acquiring data instead of using the original page_data queryset
        Get the front page style with page_obj.page_html
        """
        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

        # 总页码
        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):
         # if the total p <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

            # This page 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 - self.pager_count + 1
                else:
                    pager_start = self.current_page - self.pager_count_half
                    pager_end = self.current_page + self.pager_count_half + 1

        page_html_list = []
         # added foregoing and nav ul Label 
        page_html_list.append ( '' '
                    <nav aria-label='Page navigation>'
                    <ul class='pagination'>
                ''')
        first_page = '<li><a href="?page=%s">首页</a></li>' % (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="?page=%s">上一页</a></li>' % (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="?page=%s">%s</a></li>' % (i, i,)
            else:
                temp = '<li><a href="?page=%s">%s</a></li>' % (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="?page=%s">下一页</a></li>' % (self.current_page + 1,)
        page_html_list.append(next_page)

        last_page = '<li><a href="?page=%s">尾页</a></li>' % (self.all_pager,)
        page_html_list.append(last_page)
        # Tail add tags 
        page_html_list.append ( '' '
                                           </nav>
                                           </ul>
                                       ''')
        return ''.join(page_html_list)

use:

rear end:

= book_list models.Book2.objects.all ()
 # total number of data 
ALL_COUNT are = book_list.count ()
 # this page 
current_page = request.GET.get ( ' Page ' ,. 1 )
 # example of a page object 
page_obj = my_page. pagination (current_page = current_page, ALL_COUNT are = ALL_COUNT are)
 # total data slice 
page_queryset = book_list [page_obj.start: page_obj.end]

Front-end template:

{{Page_obj.page_html | safe}} # help you render a pager with a bootstrap style

 

Guess you like

Origin www.cnblogs.com/xufengfan/p/11026058.html