Django之自定義分頁組件

一.分頁邏輯

from django.utils.safestring import mark_safe  #mark_safe:安全字符串

class MyPage:

    def __init__(self, page_num, page_num_count, req_path, per_page_num, page_num_show):

        self.page_num = page_num
        self.page_num_count = page_num_count
        self.req_path = req_path
        self.per_page_num = per_page_num  # 每頁顯示多少條數據
        self.page_num_show = page_num_show  # 顯示的頁碼數

        # 如果有異常就默認當前頁
        try:
            self.page_num = int(self.page_num)
        except Exception:
            self.page_num = 1

        # 計算有多少頁 如果商不爲0頁數就加一 總的頁碼數
        a, b = divmod(self.page_num_count, self.per_page_num)
        if b:
            self.page_num_count = a + 1
        else:
            self.page_num_count = a

        # 如果小於1就讓他去當前頁,大於總頁數就讓他去最後一頁
        if self.page_num <= 0:
            self.page_num = 1
        elif self.page_num >= self.page_num_count:
            self.page_num = self.page_num_count

        # 每頁展示的頁碼數
        half_show = self.page_num_show // 2  # 2
        if self.page_num - half_show <= 0:
            start_page_num = 1
            end_page_num = self.page_num_show + 1
        elif self.page_num + half_show >= self.page_num_count:
            start_page_num = self.page_num_count - self.page_num_show + 1
            end_page_num = self.page_num_count + 1
        else:
            start_page_num = self.page_num - half_show  # 3
            end_page_num = self.page_num + half_show + 1  # 7

        self.start_page_num = start_page_num
        self.end_page_num = end_page_num

    # 每頁顯示多少條數據  開始
    @property
    def start_data_num(self):
        return (self.page_num - 1) * self.per_page_num

    # 每頁顯示多少條數據  結束
    @property
    def end_data_num(self):
        return self.page_num * self.per_page_num

    # 前端頁碼
    def page_html(self):

        page_num_range = range(self.start_page_num, self.end_page_num)
        page_html = ''

        # 上一頁
        if self.page_num <= 1:
            page_pre_html = f'''
                    <nav aria-label="Page navigation">
                    <ul class="pagination">
                    <li class="disabled">
                    <a href="{self.req_path}?page={1}" aria-label="Previous">
                        <span aria-hidden="true">首頁</span>
                      </a>
                      <a href="javascript:void(0)" aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                      </a>
                    </li>
                '''
        else:
            page_pre_html = f'''
                    <nav aria-label="Page navigation">
                    <ul class="pagination">
                    <li>
                    <a href="{self.req_path}?page={1}" aria-label="Previous">
                        <span aria-hidden="true">首頁</span>
                      </a>
                      <a href="{self.req_path}?page={self.page_num - 1}" aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                      </a>
                    </li>
                    '''
        page_html += page_pre_html

        # 頁碼標籤 並且有選中狀態
        for i in page_num_range:
            if i == self.page_num:
                page_html += f'<li class="active"><a href="{self.req_path}?page={i}">{i}</a></li>'
            else:
                page_html += f'<li><a href="{self.req_path}?page={i}">{i}</a></li>'

        # 下一頁
        if self.page_num >= self.page_num_count:
            page_next_html = f'''
                        <li class="disabled">
                          <a href="javascript:void(0)" aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                          </a>
                            <a href="{self.req_path}?page={self.page_num_count}" aria-label="Next">
                            <span aria-hidden="true">尾頁</span>
                          </a>
                        </li>
                      </ul>
                    </nav>
                '''
        else:
            page_next_html = f'''
                        <li>
                          <a href="{self.req_path}?page={self.page_num + 1}" aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                          </a>
                            <a href="{self.req_path}?page={self.page_num_count}" aria-label="Next">
                            <span aria-hidden="true">尾頁</span>
                          </a>
                        </li>
                      </ul>
                    </nav>
                        '''

        page_html += page_next_html

        return mark_safe(page_html)

二.首頁邏輯

def index(request):

    # 當前頁
    page_num = request.GET.get('page')
    # 獲取路徑
    req_path = request.path_info
    # 計算有多少條數據
    page_num_count = app04.models.Customer.objects.all().count()
    #settings中封裝的頁碼數和每頁顯示多少條數據
    per_page_num = settings.PER_PAGE_NUM  # 每頁顯示多少條數據
    page_num_show = settings.PAGE_NUM_SHOW  # 顯示的頁碼數
    #封裝的分頁組件
    page_obj = MyPage(page_num,page_num_count,req_path,per_page_num,page_num_show)
    page_html = page_obj.page_html()

    # 獲取所有的數據
    index_obj = app04.models.Index.objects.all()[page_obj.start_data_num:page_obj.end_data_num]
    return render(request, 'crm/index.html', {'index_obj': index_obj,'page_html':page_html})

直接拿去用就行…

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章