最初的版本html
views.pydjango
from django.shortcuts import render, HttpResponse, redirect
from django.utils.safestring import mark_safe
practice_pagination_data = [{'name': 'ward{}'.format(i), 'pwd': 'pwd{}'.format(i)} for i in range(1, 302)] # 練習分頁 def practice_pagination(request): # 當期頁面 try: current_page = int(request.GET.get('page', 1)) print(current_page) if current_page <= 0: current_page = 1 except Exception as e: current_page = 1 print(e) # 最多顯示的頁碼數 max_show = 9 # 在當前的頁面左右各顯示通常的數據 half_show = max_show // 2 # 每頁顯示的數據條數 per_num = 10 # 總數據量 all_count = len(practice_pagination_data) # 頁碼總數 total_num, more = divmod(all_count, per_num) # 若是有餘數 if more: total_num += 1 # 總頁碼數小於最大顯示數:顯示總頁數 if total_num < max_show: page_start = 1 page_end = total_num else: # 總頁碼數大於最大顯示數:最多顯示11個 if current_page <= half_show: page_start = 1 page_end = max_show elif current_page + half_show >= total_num: page_end = total_num page_start = total_num - max_show + 1 else: page_start = current_page - half_show page_end = current_page + half_show # 存放<li>標籤的列表(目的是爲了將全部的事情在服務端都作好,返回html字符串就ok) html_list = [] # 首頁操做 first_li = '<li><a href="/practice_pagination/?page=1">首頁</a></li>' html_list.append(first_li) # 若是已經到了第一頁,再點擊上一頁是不被容許的, if current_page == 1: prev_li = '<li class="disabled"><a>上一頁</a></li>' else: prev_li = '<li><a href="/practice_pagination/?page={0}">上一頁</a></li>'.format(current_page - 1) html_list.append(prev_li) # 將切片範圍內數據展現 for num in range(page_start, page_end + 1): # 若是是當前頁面。將此標籤選中 if current_page == num: li_html = '<li class="active"><a href="/practice_pagination/?page={0}">{0}</a></li>'.format(num) else: li_html = '<li><a href="/practice_pagination/?page={0}">{0}</a></li>'.format(num) html_list.append(li_html) # 當前頁面爲最後一頁,不能再點擊下一頁 if current_page == total_num: next_li = '<li class="disabled"><a>下一頁</a></li>' else: next_li = '<li><a href="practice_pagination"?page={}>下一頁</a></li>'.format(current_page + 1) html_list.append(next_li) # 尾頁操做 last_li = '<li><a href="/practice_pagination/?page={}">尾頁</a></li>'.format(total_num) html_list.append(last_li) # 將列表組成字符串 html_str = mark_safe(''.join(html_list)) # 切片的起始值 start = (current_page - 1) * per_num print(start) # 切片的終止值 end = current_page * per_num print(end) return render(request, 'practice_pagination.html', { 'data': practice_pagination_data[start:end], 'html_str': html_str, })
<div class="panel panel-default"> <div class="panel-heading">用戶信息</div> <div class="panel-body"> <table class="table table-hover table-bordered"> <thead> <tr> <th>序號</th> <th>用戶名</th> <th>密碼</th> </tr> </thead> <tbody> {% for user in data %} <tr> <td>{{ forloop.counter }}</td> <td>{{ user.name }}</td> <td>{{ user.pwd }}</td> </tr> {% endfor %} </tbody> </table> {# 分頁的模板代碼,用的是Bootstrap分頁樣式 #} <nav aria-label="Page navigation" class="navbar-right" style="margin-right: 5px"> <ul class="pagination"> {# 經過後端返回一個字符串(是html字符串,渲染到這裏) #} {{ html_str }} </ul> </nav> </div> </div>
進階版--將這個分頁寫爲本身的一個模塊後端
pagination.py
""" 分頁器 """ from django.utils.safestring import mark_safe class Pagination: # request 爲request請求, all_count爲全部數據的個數, per_num爲一頁展現多少數據, max_show分多少頁 def __init__(self, request, all_count, per_num=10, max_show=11): # 基本的URL self.base_url = request.path_info # 當前頁碼 try: self.current_page = int(request.GET.get('page', 1)) if self.current_page <= 0: self.current_page = 1 except Exception as e: self.current_page = 1 print(e) # 最多顯示的頁碼數 self.max_show = max_show half_show = max_show // 2 # 每頁顯示的數據條數 self.per_num = per_num # 總數據量 self.all_count = all_count # 總頁碼數 self.total_num, more = divmod(all_count, per_num) if more: self.total_num += 1 # 總頁碼數小於最大顯示數:顯示總頁碼數 if self.total_num <= max_show: self.page_start = 1 self.page_end = self.total_num else: # 總頁碼數大於最大顯示數:最多顯示11個 if self.current_page <= half_show: self.page_start = 1 self.page_end = max_show elif self.current_page + half_show >= self.total_num: self.page_end = self.total_num self.page_start = self.total_num - max_show + 1 else: self.page_start = self.current_page - half_show self.page_end = self.current_page + half_show @property def start(self): return (self.current_page - 1) * self.per_num @property def end(self): return self.current_page * self.per_num @property def show_li(self): # 存放li標籤的列表 html_list = [] first_li = '<li><a href="{}?page=1">首頁</a></li>'.format(self.base_url) html_list.append(first_li) if self.current_page == 1: prev_li = '<li class="disabled"><a><<</a></li>' else: prev_li = '<li><a href="{1}?page={0}"><<</a></li>'.format(self.current_page - 1, self.base_url) html_list.append(prev_li) for num in range(self.page_start, self.page_end + 1): if self.current_page == num: li_html = '<li class="active"><a href="{1}?page={0}">{0}</a></li>'.format(num, self.base_url) else: li_html = '<li><a href="{1}?page={0}">{0}</a></li>'.format(num, self.base_url) html_list.append(li_html) if self.current_page == self.total_num: next_li = '<li class="disabled"><a>>></a></li>' else: next_li = '<li><a href="{1}?page={0}">>></a></li>'.format(self.current_page + 1, self.base_url) html_list.append(next_li) last_li = '<li><a href="{1}?page={0}">尾頁</a></li>'.format(self.total_num, self.base_url) html_list.append(last_li) return mark_safe(''.join(html_list))
users = [{'name': 'ward{}'.format(i), 'pwd': 'pwd{}'.format(i)} for i in range(1, 302)] from utils import pagination def user_list(request): page = pagination.Pagination(request, len(users), per_num=13, max_show=8) return render(request, 'user_list.html', { "data": users[page.start:page.end], 'html_str': page.show_li })
<table class="table table-bordered"> <thead> <tr> <th>序號</th> <th>用戶名</th> <th>密碼</th> </tr> </thead> <tbody> {% for user in data %} <tr> <td>{{ forloop.counter }}</td> <td>{{ user.name }}</td> <td>{{ user.pwd }}</td> </tr> {% endfor %} </tbody> </table> <nav aria-label="Page navigation" class="navbar-right"> <ul class="pagination"> {# {% for num in total_num %}#} {# <li><a href="/user_list/?page={{ num }}">{{ num }}</a></li>#} {# {% endfor %}#} {# {{ html_str|safe }}#} {{ html_str }} </ul> </nav>