分页
from django.utils.safestring import mark_safe class MyPagenation(): """ page_num:当前页码 total_count:总数据量 per_page_num:每页显示数据量 page_num_show:每页显示的页码数量 base_url:当前对应的URL, href="{1}?page={0}".format(1, self.base_url) """ def __init__(self,current_page,total_count,per_page_num,page_num_show,base_url): # 每页显示10条数据 self.per_page_num = per_page_num # 页面生成页码的数量 self.page_num_show = page_num_show # 7 4 5 6 7 8 self.base_url = base_url # 7 4 5 6 7 8 try: page_num = int(current_page) except Exception: page_num = 1 #当前页码 self.page_num = page_num shang, yu = divmod(total_count, self.per_page_num) # shang:商 yu:余数 # 总页码数 if yu: page_num_count = shang + 1 else: page_num_count = shang self.page_num_count = page_num_count if page_num <= 0: page_num = 1 elif page_num > page_num_count: page_num = page_num_count # 3 4 5 6 7 4 5 6 7 8 9 10 half_show = self.page_num_show // 2 # 2 if page_num - half_show <= 0: start_page_num = 1 end_page_num = self.page_num_show + 1 elif page_num + half_show > page_num_count: start_page_num = page_num_count - self.page_num_show + 1 # 26 - 5 = 21 end_page_num = page_num_count + 1 # 27 [21,22,23,24,25,26] else: start_page_num = page_num - half_show # 4 1 end_page_num = page_num + half_show + 1 # 9 6 """ 添加此处,修改 """ if self.page_num_count<self.page_num_show: start_page_num = 1 end_page_num = self.page_num_count + 1 ############# self.start_page_num = start_page_num self.end_page_num = end_page_num @property def start_data_num(self): return (self.page_num - 1) * self.per_page_num @property def end_data_num(self): return self.page_num * self.per_page_num def page_hmtl(self): page_num_range = range(self.start_page_num, self.end_page_num) page_html = '' page_pre_html = '<nav aria-label="Page navigation"><ul class="pagination">' page_html += page_pre_html first_page_html = '<li><a href="{1}?page={0}" aria-label="Previous"><span aria-hidden="true">首页</span></a></li>'.format(1, self.base_url) page_html += first_page_html if self.page_num <= 1: pre_page = '<li class="disabled"><a href="javascript:void(0)" aria-label="Previous"><span aria-hidden="true">上一页</span></a></li>'.format(self.page_num - 1) else: pre_page = '<li><a href="{1}?page={0}" aria-label="Previous"><span aria-hidden="true">上一页</span></a></li>'.format(self.page_num - 1,self.base_url) page_html += pre_page for i in page_num_range: if i == self.page_num: page_html += '<li class="active"><a href="{1}?page={0}">{0}</a></li>'.format(i,self.base_url) else: page_html += '<li><a href="{1}?page={0}">{0}</a></li>'.format(i,self.base_url) if self.page_num >= self.page_num_count: page_next_html = '<li class="disabled"><a href="javascript:void(0)" aria-label="Next"><span aria-hidden="true">下一页</span></a></li>'.format( self.page_num + 1) else: page_next_html = '<li><a href="{1}?page={0}" aria-label="Next"><span aria-hidden="true">下一页</span></a></li>'.format( self.page_num + 1,self.base_url) page_html += page_next_html last_page_html = '<li><a href="{1}?page={0}" aria-label="Previous"><span aria-hidden="true">尾页</span></a></li>'.format( self.page_num_count, self.base_url) page_html += last_page_html end_html = '</ul></nav>' page_html += end_html return mark_safe(page_html)
{{ page_html }}
2、添加搜索条件的分页
request.path: -- 获取当前url,(但不含参数) request.get_full_path() -- 获取当前url,(包含参数) request.GET.urlencode():urlencode() -- 以一个查询字符串的形式返回一个字符串. Example: "a=2&b=3&b=5".
其中request.GET为QueryDict类型 `QueryDict`` 还拥有下列方法: copy() -- 返回当前对象的一个拷贝,它使用标准库中的 深拷贝 方法. 这个拷贝是可变的,也就是说你可以改变这个拷贝的值. getlist(key) -- 以一个Python列表的形式返回指定键的值.若该键不存在,返回一个空的列表.该列表是以某种方式排序的. setlist(key, list_) -- 不同于 __setitem__() ,将给定的键的值设置为一个列表. appendlist(key, item) -- 将给定键对应的值(别忘了,它是一个列表)追加一个 item. setlistdefault(key, default_list) -- 就象 setdefault ,不过它接受一个列表作为值而不是一个单一的值. lists() -- 就象 items(),不过它包含所有的值(以列表的方式): >>> q = QueryDict('a=1&a=2&a=3') >>> q.lists() [('a', ['1', '2', '3'])]
地址栏保留分页标签和搜索条件标签
from django.utils.safestring import mark_safe
class MyPagenation():
"""
page_num:当前页码
total_count:总数据量
per_page_num:每页显示数据量
page_num_show:每页显示的页码数量
base_url:当前对应的URL, href="{1}?page={0}".format(1, self.base_url)
"""
def __init__(self,current_page,total_count,per_page_num,page_num_show,base_url,get_data=None):
# 每页显示10条数据
self.per_page_num = per_page_num
# 页面生成页码的数量
self.page_num_show = page_num_show # 7 4 5 6 7 8
self.base_url = base_url # 7 4 5 6 7 8
try:
page_num = int(current_page)
except Exception:
page_num = 1
#当前页码
self.page_num = page_num
#搜索条件
self.get_data=get_data
shang, yu = divmod(total_count, self.per_page_num) # shang:商 yu:余数
# 总页码数
if yu:
page_num_count = shang + 1
else:
page_num_count = shang
self.page_num_count = page_num_count
if page_num <= 0:
page_num = 1
elif page_num > page_num_count:
page_num = page_num_count
# 3 4 5 6 7 4 5 6 7 8 9 10
half_show = self.page_num_show // 2 # 2
if page_num - half_show <= 0:
start_page_num = 1
end_page_num = self.page_num_show + 1
elif page_num + half_show > page_num_count:
start_page_num = page_num_count - self.page_num_show + 1 # 26 - 5 = 21
end_page_num = page_num_count + 1 # 27 [21,22,23,24,25,26]
else:
start_page_num = page_num - half_show # 4 1
end_page_num = page_num + half_show + 1 # 9 6
"""
添加此处,修改
"""
if self.page_num_count<self.page_num_show:
start_page_num = 1
end_page_num = self.page_num_count+1
self.start_page_num = start_page_num
self.end_page_num = end_page_num
@property
def start_data_num(self):
return (self.page_num - 1) * self.per_page_num
@property
def end_data_num(self):
return self.page_num * self.per_page_num
def page_hmtl(self):
import re
page_num_range = range(self.start_page_num, self.end_page_num)
page_html = ''
page_pre_html = '<nav aria-label="Page navigation"><ul class="pagination">'
page_html += page_pre_html
self.get_data['page'] = 1
first_page_html = '<li><a href="{1}?{0}" aria-label="Previous"><span aria-hidden="true">首页</span></a></li>'.format(self.get_data.urlencode(), self.base_url)
page_html += first_page_html
if self.page_num <= 1:
pre_page = '<li class="disabled"><a href="javascript:void(0)" aria-label="Previous"><span aria-hidden="true">«</span></a></li>'.format(self.page_num - 1)
else:
self.get_data['page'] = self.page_num - 1
pre_page = '<li><a href="{1}?{0}" aria-label="Previous"><span aria-hidden="true">«</span></a></li>'.format(self.get_data.urlencode(),self.base_url)
page_html += pre_page
for i in page_num_range:
if i == self.page_num:
self.get_data['page']=i
page_html += '<li class="active"><a href="{1}?{2}">{0}</a></li>'.format(i,self.base_url,self.get_data.urlencode())
else:
self.get_data['page'] = i
page_html += '<li><a href="{1}?{2}">{0}</a></li>'.format(i,self.base_url,self.get_data.urlencode())
if self.page_num >= self.page_num_count:
page_next_html = '<li class="disabled"><a href="javascript:void(0)" aria-label="Next"><span aria-hidden="true">»</span></a></li>'.format(
self.page_num + 1)
else:
self.get_data["page"]=self.page_num + 1
page_next_html = '<li><a href="{1}?{0}" aria-label="Next"><span aria-hidden="true">»</span></a></li>'.format(
self.get_data.urlencode(),self.base_url)
page_html += page_next_html
self.get_data["page"] = self.page_num_count
last_page_html = '<li><a href="{1}?{0}" aria-label="Previous"><span aria-hidden="true">尾页</span></a></li>'.format(
self.get_data.urlencode(), self.base_url)
page_html += last_page_html
end_html = '</ul></nav>'
page_html += end_html
return mark_safe(page_html)
def customers(request):
if request.session.get("is_login",None):
current_page=request.GET.get("page",1)
from superCRMPratice import settings
per_page_num=settings.PER_PAGE_NUM
page_num_show=settings.PAGE_NUM_SHOW
base_url=request.path
from django.http.request import QueryDict
#import copy
#get_data=copy.copy(request.GET)
get_data = request.GET.copy() #将request.GET对象改成可修改的
keywords = request.GET.get('keywords', None)
search_field=request.GET.get('search_field')
from django.db.models import Q
if keywords:
q_obj=Q()
q_obj.connector='or'
q_obj.children.append((search_field,keywords))#Q(name=key)
customer_list = models.Customer.objects.filter(q_obj)
else:
customer_list = models.Customer.objects.all()
customer_whole_count = customer_list.count()
page_obj=MyPagenation(current_page,customer_whole_count,per_page_num,page_num_show,base_url,get_data)
customer_list=customer_list.reverse()[page_obj.start_data_num:page_obj.end_data_num]
return render(request, "sales/customers.html", {'customer_list': customer_list,'page_html':page_obj.page_hmtl()})
else:
return redirect("/login/")
<a href="{% url 'add_customer' %}" class="btn btn-primary">添加</a>
<form class="form-inline"action="{% url 'customers' %}"method="get">
<select name="search_field"id="search-field"class="form-control">
<option value="qq__contains"selected>qq</option>
<option value="name__icontains">name</option>
</select>
<div class="form-group">
<input type="text" class="form-control" name="keywords" id="keyword" placeholder="请输入搜索内容">
</div>
<button type="submit" class="btn btn-default">搜索</button>
</form>
<table class="table table-striped table-hover table-bordered">
<thead>
<tr>
<th>序号</th>
<th>ID</th>
<th>QQ</th>
<th>姓名</th>
<th>性别</th>
<th>转自介绍学员</th>
<th>状态</th>
<th>销售</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for item in customer_list %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ item.pk }}</td>
<td>{{ item.qq }}</td>
<td>{{ item.name }}</td>
<td>
{% if item.sex == 'male' %}
男
{% else %}
女
{% endif %}
</td>
<td>{{ item.introduce_from|default:"无" }}</td>
<td>{{ item.status_show }}</td>
<td>{{ item.consultant|default:'暂无' }}</td>
<td><a href="{% url 'edit_customer' item.pk %}"><i class="fa fa-edit"></i></a></td>
</tr>
{% endfor %}
</tbody>
</table>
{{ page_html }}

浙公网安备 33010602011771号