django實現分頁的模組(匯入即可用)

龙岩龙發表於2024-09-15

`from django.utils.safestring import mark_safe
import copy

class PagerPlay(object):
def init(self,request,customer_list):
self.customer_list = customer_list
self.request_GET = copy.deepcopy(request.GET)
self.request_GET._mutable = True
self.page = request.GET.get("page")
if not self.page:
self.page = 1
self.start = (int(self.page) - 1) * 10
self.end = int(self.page) * 10
self.total_page, self.div = divmod(self.customer_list.count(), 10)
self.query_list = self.customer_list[self.start:self.end]
self.page_list = []
def page_html(self):
if self.div:
self.total_page += 1

    self.request_GET.setlist('page', [1])
    self.page_list.append(
        '<li class="page-item"><a class="page-link" href="?{}"  style="text-align: center;">首</a></li>'.format(
            self.request_GET.urlencode()))
    if int(self.page) > 1:
        self.request_GET.setlist('page', [int(self.page) - 1])
        self.page_list.append(
            '<li class="page-item"><a class="page-link" href="?{}"  style="text-align: center;"> <</a></li>'.format(
                self.request_GET.urlencode()))

    if self.total_page < 11:
        start_page = 1
        end_page = self.total_page
        for i in range(start_page, end_page + 1):
            if i == int(self.page):
                self.request_GET.setlist('page', [i])
                item = '<li class="page-item active"><a class="page-link" href="?{}" style="text-align: center">{}</a></li>'.format(
                    self.request_GET.urlencode(), i)
                self.page_list.append(item)
                continue
            self.request_GET.setlist('page', [i])
            item = '<li class="page-item"><a class="page-link" href="?page={}" style="text-align: center">{}</a></li>'.format(
                self.request_GET.urlencode(), i)
            self.page_list.append(item)
            self.request_GET.setlist('page', [self.total_page])
            self.page_list.append(
                '<li class="page-item"><a class="page-link" href="?{}"  style="text-align: center;">尾</a></li>'.format(
                    self.request_GET.urlencode()))
        page_ll = mark_safe(''.join(self.page_list))
        return page_ll
    if self.total_page > 11:
        if int(self.page) < 6:
            start_page = 1
            end_page = 11
        else:
            if int(self.page) + 5 > self.total_page:
                start_page = self.total_page - 10
                end_page = self.total_page
            else:
                start_page = int(self.page) - 5
                end_page = int(self.page) + 5
        for i in range(start_page, end_page + 1):
            if i == int(self.page):
                self.request_GET.setlist('page', [i])

                item = '<li class="page-item active"><a class="page-link" href="?{}" style="text-align: center">{}</a></li>'.format(
                    self.request_GET.urlencode(), i)

                self.page_list.append(item)
                continue
            self.request_GET.setlist('page', [i])
            item = '<li class="page-item"><a class="page-link" href="?{}" style="text-align: center">{}</a></li>'.format(
                self.request_GET.urlencode(), i)
            self.page_list.append(item)
        if int(self.page) < self.total_page:
            self.request_GET.setlist('page', [int(self.page) + 1])
            self.page_list.append(
                '<li class="page-item"><a class="page-link" href="?{}"  style="text-align: center;">></a></li>'.format(
                    self.request_GET.urlencode()))
        self.request_GET.setlist('page', [self.total_page])
        self.page_list.append(
            '<li class="page-item"><a class="page-link" href="?page={}"  style="text-align: center;">尾</a></li>'.format(
                self.request_GET.urlencode()))
        page_ll = mark_safe(''.join(self.page_list))
        return page_ll`

相關文章