python__Django 分頁

來源:互聯網
上載者:User

標籤:lis   def   django   nat   active   style   效果   使用   int   

自訂分頁的類:

#!/usr/bin/env python# -*- coding: utf-8 -*-# Created by Mona on 2017/9/20from django.utils.safestring import mark_safeclass Paginator:    ‘‘‘    頁碼的格式依賴於bootstrap;    使用案例:            from django.shortcuts import render,redirect,HttpResponse    from app01.models import *    from tools import page  匯入            def index(request):        base_url = request.path_info        total_item_count = UserInfo.objects.all().count()        current_page = int(request.GET.get(‘page‘,1))            page_obj = page.Paginator(total_item_count,current_page,base_url)            user_list = UserInfo.objects.all()[page_obj.start:page_obj.end]            return render(request,‘index.html‘,{‘user_list‘:user_list,‘html_page‘: page_obj.pager})        ‘‘‘    def __init__(self,total_item_count,current_page,base_url,
          per_page_item_count=10,show_pager_count=11): ‘‘‘ :param total_item_count: 總記錄數 :param current_page: 當前頁碼 :param base_url: 頁碼的首碼URL :param per_page_item_count: 每頁顯示的記錄數 :param show_pager_count: 顯示的頁碼個數 ‘‘‘ self.total_item_count = total_item_count self.current_page = current_page self.base_url = base_url self.per_page_item_count = per_page_item_count self.show_pager_count = show_pager_count total_page, res = divmod(self.total_item_count, self.per_page_item_count) if res: total_page += 1 self.total_page = total_page self.half_show_pager_count = int(total_page/2) @property def start(self): ‘‘‘ 頁碼查詢資料的開始位置 :return: ‘‘‘ return (self.current_page-1)*self.per_page_item_count @property def end(self): ‘‘‘ 頁碼查詢資料的結束位置 :return: ‘‘‘ return self.current_page*self.per_page_item_count @property def pager(self): ‘‘‘ 返回需要的前端頁碼 :return: ‘‘‘ page_list = [] ul = ‘ <ul class="pagination">‘ page_list.append(ul) if self.current_page == 1: prev = ‘ <li><a href="#">上一頁</a></li>‘ else: prev = ‘ <li><a href="%s?page=%s">上一頁</a></li>‘ % (self.base_url, self.current_page - 1) page_list.append(prev) # 如果資料特別少: if self.total_page < self.show_pager_count: pager_start = 1 pager_end = self.total_page + 1 else: if self.current_page <= self.half_show_pager_count: pager_start = 1 pager_end = self.show_pager_count + 1 else: if self.current_page + self.half_show_pager_count > self.total_page: pager_start = self.total_page - self.show_pager_count + 1 pager_end = self.total_page + 1 else: pager_start = self.current_page - self.half_show_pager_count pager_end = self.current_page + self.half_show_pager_count + 1 for i in range(pager_start, pager_end): if i == self.current_page: tpl = ‘ <li class="active"><a href="%s?page=%s">%s</a></li>‘ % (self.base_url, i, i,) else: tpl = ‘ <li><a href="%s?page=%s">%s</a></li>‘ % (self.base_url, i, i,) page_list.append(tpl) if self.current_page == self.total_page: nex = ‘ <li><a href="#">下一頁</a></li>‘ else: nex = ‘ <li><a href="%s?page=%s">下一頁</a></li>‘ % (self.base_url, self.current_page + 1,) page_list.append(nex) lu = ‘ </ul>‘ page_list.append(lu) html_page = mark_safe(‘‘.join(page_list)) return html_page

效果展示:

python__Django 分頁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.