最初的版本
views.py
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>