函数形式
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'上一页 ')
else:
page_list.append(f'上一页 ')
for page in range(page_start,page_end + 1):
page_list.append(f'{page} ')
# 控制页数不超过总页数
if page_num == page_count:
page_list.append(f'下一页 ')
else:
page_list.append(f'下一页 ')
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 %}
客户信息展示
{% endblock %}
{% block content %}
添加
序号
姓名
性别
出生日期
QQ
手机号
咨询课程
客户状态
操作
{% for custobj in cust_obj %}
{{ forloop.counter }}
{{ custobj.name }}
{{ custobj.sex }}
{{ custobj.birthday|date:"Y-m-d" }}
{{ custobj.qq }}
{{ custobj.phone }}
{{ custobj.course }}
{{ custobj.status }}
{% endfor %}
{% endblock %}
{% block 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'首页 ')
page_list.append(f'上一页 ')
else:
page_list.append(f'首页 ')
page_list.append(f'上一页 ')
for page in range(page_start, page_end + 1):
if page == self.page_num:
page_list.append(f'{page} ')
else:
page_list.append(f'{page} ')
# 控制页数不超过总页数
if self.page_num == self.page_count:
page_list.append(f'下一页 ')
page_list.append(f'尾页 ')
else:
page_list.append(f'下一页 ')
page_list.append(f'尾页 ')
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})