1. 程式人生 > >第八篇Django分頁

第八篇Django分頁

name 數據庫中獲取數據 alex 頁面 base 指南 init RM req

Django分頁

1.復雜版

技術分享圖片
data = []

for i in range(1, 302):
    tmp = {"id": i, "name": "alex-{}".format(i)}
    data.append(tmp)

print(data)


def user_list(request):

    # user_list = data[0:10]
    # user_list = data[10:20]
    try:
        current_page = int(request.GET.get("page"))
    except Exception as
e: current_page = 1 per_page = 10 # 數據總條數 total_count = len(data) # 總頁碼 total_page, more = divmod(total_count, per_page) if more: total_page += 1 # 頁面最多顯示多少個頁碼 max_show = 11 half_show = int((max_show-1)/2) if current_page <= half_show: show_start
= 1 show_end = max_show else: if current_page + half_show >= total_page: show_start = total_page - max_show show_end = total_page else: show_start = current_page - half_show show_end = current_page + half_show # 數據庫中獲取數據 data_start
= (current_page - 1) * per_page data_end = current_page * per_page user_list = data[data_start:data_end] # 生成頁面上顯示的頁碼 page_html_list = [] # 加首頁 first_li = <li><a href="/user_list/?page=1">首頁</a></li> page_html_list.append(first_li) # 加上一頁 if current_page == 1: prev_li = <li><a href="#">上一頁</a></li> else: prev_li = <li><a href="/user_list/?page={}">上一頁</a></li>.format(current_page - 1) page_html_list.append(prev_li) for i in range(show_start, show_end+1): if i == current_page: li_tag = <li class="active"><a href="/user_list/?page={0}">{0}</a></li>.format(i) else: li_tag = <li><a href="/user_list/?page={0}">{0}</a></li>.format(i) page_html_list.append(li_tag) # 加下一頁 if current_page == total_page: next_li = <li><a href="#">下一頁</a></li> else: next_li = <li><a href="/user_list/?page={}">下一頁</a></li>.format(current_page+1) page_html_list.append(next_li) # 加尾頁 page_end_li = <li><a href="/user_list/?page={}">尾頁</a></li>.format(total_page) page_html_list.append(page_end_li) page_html = "".join(page_html_list) return render(request, "user_list.html", {"user_list": user_list, "page_html": page_html})
復雜版

2.封裝保存版

技術分享圖片
class Pagination(object):
    def __init__(self, current_page, total_count, base_url, per_page=10, max_show=11):
        """
        :param current_page: 當前頁
        :param total_count: 數據庫中數據總數
        :param per_page: 每頁顯示多少條數據
        :param max_show: 最多顯示多少頁
        """
        try:
            current_page = int(current_page)
        except Exception as e:
            current_page = 1

        self.current_page = current_page
        self.total_count = total_count
        self.base_url = base_url
        self.per_page = per_page
        self.max_show = max_show

        # 總頁碼
        total_page, more = divmod(total_count, per_page)
        if more:
            total_page += 1
        
        half_show = int((max_show - 1) / 2)
        self.half_show = half_show
        self.total_page = total_page

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

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

    def page_html(self):

        if self.current_page <= self.half_show:
            show_start = 1
            show_end = self.max_show
        else:
            if self.current_page + self.half_show >= self.total_page:
                show_start = self.total_page - self.max_show
                show_end = self.total_page
            else:
                show_start = self.current_page - self.half_show
                show_end = self.current_page + self.half_show

                # 生成頁面上顯示的頁碼
        page_html_list = []
        # 加首頁
        first_li = <li><a href="{}?page=1">首頁</a></li>.format(self.base_url)
        page_html_list.append(first_li)
        # 加上一頁
        if self.current_page == 1:
            prev_li = <li><a href="#">上一頁</a></li>
        else:
            prev_li = <li><a href="{0}?page={1}">上一頁</a></li>.format(self.base_url, self.current_page - 1)
        page_html_list.append(prev_li)
        for i in range(show_start, show_end + 1):
            if i == self.current_page:
                li_tag = <li class="active"><a href="{0}?page={1}">{1}</a></li>.format(self.base_url, i)
            else:
                li_tag = <li><a href="{0}?page={1}">{1}</a></li>.format(self.base_url, i)
            page_html_list.append(li_tag)

        # 加下一頁
        if self.current_page == self.total_page:
            next_li = <li><a href="#">下一頁</a></li>
        else:
            next_li = <li><a href="{0}?page={1}">下一頁</a></li>.format(self.base_url, self.current_page + 1)
        page_html_list.append(next_li)

        # 加尾頁
        page_end_li = <li><a href="{0}?page={1}">尾頁</a></li>.format(self.base_url, self.total_page)
        page_html_list.append(page_end_li)

        return "".join(page_html_list)
封裝保存版

3.封裝保存版使用指南

技術分享圖片
def user_list(request):
    pager = Pagination(request.GET.get("page"), len(data), request.path_info)
    user_list = data[pager.start:pager.end]
    page_html = pager.page_html()
    return render(request, "user_list.html", {"user_list": user_list, "page_html": page_html})
View Code

第八篇Django分頁