分页
第一步在视图中写(在get方法中得到:page,per_page,total_num)
def shop_info_view(request):
section = '店铺信息'
if request.method == 'GET':
shopinfo = ShopInfo.objects.filter(logic_delete=False)
total_num = shopinfo.count()
if request.GET.get('page'):
page = int(request.GET.get('page'))
else:
page = 1
if request.GET.get('per_page'):
per_page = int(request.GET.get('per_page'))
else:
per_page = 10
shopinfo = shopinfo[(page-1)*per_page:page*per_page]
context = {
'section': section,
'shopinfo': shopinfo,
'page': page,
'per_page': per_page,
'total_num': total_num
}
return render(request, 'shopincomes/shopinfo.html', context=context)
------------------------------------------------------------------------------------------------------------------------
第二步创建一个过滤器标签(文件名:shop_info_filter.py)
from django.template import Library
import math
register = Library()
@register.inclusion_tag('shopincomes/paginate.html')
def paginate(page, per_page, total_num):
# 要显示的页码表
# 总页码
num = 3 # 总共显示多少页码,一般都是奇数
page_list = [ ] #要显示的页码
total_page = math.ceil(total_num/per_page) #向上整除,只要有小数就进一位
# 先算当前页左边的页码,看看需不需从第一页显示起
if page - (num-1)//2 <= 0:
for i in range(page):
page_list.append(i+1) #i+1,表示页码是从1开始的
else:
for i in range(page-(num-1)//2, page+1):
page_list.append(i)
# 再算右边,看看需不需要显示到最后一页
if page + num >= total_page:
for i in range(page+1, total_page+1):
page_list.append(i)
else:
for i in range(page+1, page+num+1):
page_list.append(i)
if page > 1:
has_pre = True
else:
has_pre = False
if page < total_page:
has_nex = True
else:
has_nex = False
return {'page': page,
'per_page': per_page,
'total_num': total_num,
'page_list': page_list,
'has_pre': has_pre,
'has_nex': has_nex,
}
------------------------------------------------------------------------------------------------------------
第三步新建一个html文件(文件名:paginate.html)
<ul class="pagination">
<li {% if not has_pre %}class="disabled"{% endif %}>
<a href="{% if has_pre %}{% url 'shopincomes:shop_info_view' %}?page={{ page|add:-1 }}&per_page={{ per_page }}{% endif %}" aria-label="Previous">
<span aria-hidden="true">上一页</span>
</a>
</li>
{% for item in page_list %}
<li {% if page == item %}class="active"{% endif %}><a href="{% url 'shopincomes:shop_info_view' %}?page={{ item }}&per_page={{ per_page }}">{{ item }}</a></li>
{% endfor %}
<li {% if not has_nex %}class="disabled"{% endif %}>
<a href="{% if has_nex %}{% url 'shopincomes:shop_info_view' %}?page={{ page|add:1 }}&per_page={{ per_page }}{% endif %}" aria-label="Next">
<span aria-hidden="true">下一页</span>
</a>
</li>
</ul>
<div class="btn-group" style="margin-bottom: 65px">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" >
{{ per_page }}条/页 <span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><a href="{% url 'shopincomes:shop_info_view' %}?page={{ page }}&per_page=10">10条/页</a></li>
<li><a href="{% url 'shopincomes:shop_info_view' %}?page={{ page }}&per_page=25">25条/页</a></li>
<li><a href="{% url 'shopincomes:shop_info_view' %}?page={{ page }}&per_page=50">50条/页</a></li>
<li><a href="{% url 'shopincomes:shop_info_view' %}?page={{ page }}&per_page=100">100条/页</a></li>
<li role="separator" class="divider"></li>
</ul>
</div>
----------------------------------------------------------------------------------------------------------------
第四步在页面中渲染
{% load shop_info_filter %} #导入过滤器文件
<nav aria-label="Page navigation">
<li>总共有:{{ total_num }}条数据</li>
{% paginate page per_page total_num %}
</nav>