函数形式
from django.utils.safestring import mark_safe
def index(request):
"""
客户信息
:param request:
:return:
"""
cust_obj = models.Customer.objects.all()
# 获取第几页
try:
page_num = int(request.GET.get("page"))
if page_num <= 0:
page_num = 1
except Exception:
page_num = 1
# 每页条数
number_each_page = 2
# 总数据量
total_number = cust_obj.count()
# 总页数
# divmod(a,b) 返回一个包含商和余数的元组
page_count,more = divmod(total_number,number_each_page)
if more:
page_count += 1
# 最大显示页码数量
max_page = 3
half_page= max_page // 2
# 控制显示页码数量
if page_count <= max_page:
page_start = 1
page_end = page_count
else:
if page_num <= half_page:
page_start = 1
page_end = max_page
elif page_num + half_page >= page_count:
page_start = page_count - max_page + 1
page_end = page_count
else:
page_start = page_num - half_page
page_end = page_num + half_page
# 控制页数不低于1,利用列表生成HTML,在前端展示
page_list = []
if page_num == 1:
page_list.append(f'<li class="disabled"><a href="?page={page_num}">上一页</a></li>')
else:
page_list.append(f'<li><a href="?page={page_num - 1}">上一页</a></li>')
for page in range(page_start,page_end + 1):
page_list.append(f'<li><a href="?page={page}">{page}</a></li>')
# 控制页数不超过总页数
if page_num == page_count:
page_list.append(f'<li class="disabled"><a href="?page={page_count}">下一页</a></li>')
else:
page_list.append(f'<li ><a href="?page={page_num + 1}">下一页</a></li>')
page_html = "".join(page_list)
return render(request, 'home/index.html', {"cust_obj": cust_obj[(page_num - 1) * number_each_page:page_num * number_each_page],"page_html":mark_safe(page_html)})
{% extends 'home/starter.html' %}
{% block header %}
<h1>客户信息展示</h1>
{% endblock %}
{% block content %}
<div>
<a href="/home/add" class="add btn btn-success pull-left">添加</a>
<div class="row">
<div class="col-lg-4 pull-right">
<form action="" method="get">
<div class="col-lg-3" style="left: 32px;">
<select class="form-control">
<option value="name">姓名</option>
<option value="qq">QQ</option>
</select>
</div>
<div class="col-lg-9">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search for...">
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Go!</button>
</span>
</div>
</div>
</form>
</div>
</div>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>序号</th>
<th>姓名</th>
<th>性别</th>
<th>出生日期</th>
<th>QQ</th>
<th>手机号</th>
<th>咨询课程</th>
<th>客户状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for custobj in cust_obj %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ custobj.name }}</td>
<td>{{ custobj.sex }}</td>
<td>{{ custobj.birthday|date:"Y-m-d" }}</td>
<td>{{ custobj.qq }}</td>
<td>{{ custobj.phone }}</td>
<td>{{ custobj.course }}</td>
<td>{{ custobj.status }}</td>
<td>
<a href="/home/edit/{{ custobj.id }}" class="glyphicon glyphicon-pencil"></a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<nav aria-label="Page navigation">
<ul class="pagination">
{{ page_html|safe }}
</ul>
</nav>
</div>
{% endblock %}
{% block script %}
<script>
$('[value="name"]').click(function () {
$('#select').text("姓名");
})
</script>
{% endblock %}
引用类的方式
from django.utils.safestring import mark_safe
class Paging:
def __init__(self,page_num,total_number,base_url=None,number_each_page=3,max_page=3):
"""
:param page_num: 当前所在页码
:param total_number: 总数据条数
:param base_url: 传入URL来保证在页码跳转时保存原URL信息
:param number_each_page: 每页条数
:param max_page: 最大显示页码数量
"""
# 获取当前页码
try:
self.page_num = int(page_num)
if self.page_num <= 0:
self.page_num = 1
except Exception:
self.page_num = 1
# 每页显示数据量
self.number_each_page = number_each_page
# 总数据条数
self.total_number = total_number
# 总页数
# divmod(a, b) 返回一个包含商和余数的元组(a // b, a % b)
self.page_count, more = divmod(self.total_number, self.number_each_page)
if more:
self.page_count += 1
# 最大显示页码数量
self.max_page = max_page
self.half_page = max_page // 2
# 搜索url
self.base_url = base_url
# 控制显示页码数量
@property
def page_html(self):
if self.page_count <= self.max_page:
page_start = 1
page_end = self.page_count
else:
if self.page_num <= self.half_page:
page_start = 1
page_end = self.max_page
elif self.page_num + self.half_page >= self.page_count:
page_start = self.page_count - self.max_page + 1
page_end = self.page_count
else:
page_start = self.page_num - self.half_page
page_end = self.page_num + self.half_page
# 控制页数不低于1
page_list = []
if self.page_num == 1:
page_list.append(f'<li class="disabled"><a href="?{self.base_url}&page=1">首页</a></li>')
page_list.append(f'<li class="disabled"><a href="?{self.base_url}&page={self.page_num}">上一页</a></li>')
else:
page_list.append(f'<li><a href="?{self.base_url}&page=1">首页</a></li>')
page_list.append(f'<li><a href="?{self.base_url}&page={self.page_num - 1}">上一页</a></li>')
for page in range(page_start, page_end + 1):
if page == self.page_num:
page_list.append(f'<li class="active"><a href="?{self.base_url}&page={page}">{page}</a></li>')
else:
page_list.append(f'<li><a href="?{self.base_url}&page={page}">{page}</a></li>')
# 控制页数不超过总页数
if self.page_num == self.page_count:
page_list.append(f'<li class="disabled"><a href="?{self.base_url}&page={self.page_count}">下一页</a></li>')
page_list.append(f'<li class="disabled"><a href="?{self.base_url}&page={self.page_count}">尾页</a></li>')
else:
page_list.append(f'<li ><a href="?{self.base_url}&page={self.page_num + 1}">下一页</a></li>')
page_list.append(f'<li ><a href="?{self.base_url}&page={self.page_count}">尾页</a></li>')
page_html = "".join(page_list)
return mark_safe(page_html)
@property
def start(self):
return (self.page_num - 1) * self.number_each_page
@property
def end(self):
return self.page_num * self.number_each_page
def index(request):
"""
客户信息
:param request:
:return:
"""
cust_obj = models.Customer.objects.all()
# 搜索
search_field = request.GET.get('search_field')
kw = request.GET.get('kw')
base_url = None
if kw:
kw = kw.strip()
q_obj = Q()
q_obj.children.append((search_field + "__contains", kw))
cust_obj = cust_obj.filter(q_obj)
base_url = {}
base_url["search_field"] = search_field
base_url["kw"] = kw
base_url = urlencode(base_url)
print(base_url)
page = Paging(request.GET.get("page"),cust_obj.count())
return render(request, 'home/index.html', {"cust_obj": cust_obj[page.start:page.end],"page_html":page.page_html})