P43靓号管理-分页(自己写的,重点看算法)
def pretty_list(request):
"""靓号列表"""
# select * from 表 order by level desc;
data_dict = {}
search_data = request.GET.get('q', '')
if search_data:
data_dict['mobile__contains'] = search_data
# 根据用户想要访问的页码计算出起始位置
page = int(request.GET.get('page', 1))
page_size = 10
start = (page - 1) * page_size
end = page * page_size
# 数据总条数
total_count = models.PrettyNum.objects.filter(**data_dict).order_by("-level").count()
# 总页码
total_page_count, div = divmod(total_count, page_size)
if div:
total_page_count += 1
# 计算出前后5页,后5页
plus = 5
# 数据库中的数据比较少,没有达到11页:
if total_page_count <= (2*plus+1):
start_page = page - plus
end_page = page + plus
# 数据库中的数据比较多,>11页
else:
# 往前翻页小于等于plus=5
if page <= plus:
start_page = 1
end_page = 2 * plus + 1
# 往后翻页码,当前页码+5>总页面
elif page+plus > total_page_count:
start_page = total_page_count - 2*plus
end_page = total_page_count
# 中间情况
else:
start_page = page - plus
end_page = page + plus
queryset = models.PrettyNum.objects.filter(**data_dict).order_by("-level")[start:end]
# select * from 表 order by level asc;
# models.PrettyNum.objects.all().order_by("level")
# 生成前端页码
from django.utils.safestring import mark_safe
page_str_list = []
# 首页
first_page = '<li><a href="?page=1">首页</a></li>'
page_str_list.append(first_page)
# 上一页:
if page > 1:
prev_page = '<li><a href="?page={}">上一页</a></li>'.format(page-1)
else:
prev_page = '<li><a href="?page={}">上一页</a></li>'.format(1)
page_str_list.append(prev_page)
# for i in range(1, total_page_count+1):
for i in range(start_page, end_page):
if i == page:
ele = '<li class="active"><a href="?page={}">{}</a></li>'.format(i, i)
else:
ele = '<li><a href="?page={}">{}</a></li>'.format(i, i)
page_str_list.append(ele)
# 下一页:
if page < total_page_count:
next_page = '<li><a href="?page={}">下一页</a></li>'.format(page + 1)
else:
next_page = '<li><a href="?page={}">上一页</a></li>'.format(total_page_count)
page_str_list.append(next_page)
# 尾页
last_page = '<li><a href="?page={}">尾页</a></li>'.format(total_page_count)
page_str_list.append(last_page)
page_string = "".join(page_str_list)
page_string = mark_safe(page_string)
for obj in queryset:
print(obj.mobile)
return render(request, 'pretty_list.html', {'queryset': queryset, 'search_data': search_data, "page_string": page_string})
{% extends 'layout.html' %}
{% block content %}
<div class="container">
<div style="margin-bottom: 10px" class="clearfix">
<a class="btn btn-success" href="/pretty_add/" target="_blank">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
新建靓号
</a>
<div style="float: right; width: 300px;">
{# <input type="text" placeholder="搜索靓号" name="q">#}
<form action="/pretty_list/" method="GET">
<div class="input-group">
<input type="text" class="form-control" placeholder="搜索靓号" name="q" value="{{ search_data }}">
<span class="input-group-btn">
<button class="btn btn-default" type="submit">
<span class="glyphicon glyphicon-search" aria-hidden="true"></span>
</button>
</span>
</div><!-- /input-group -->
</form>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<span class="glyphicon glyphicon-th-list" aria-hidden="true"></span>
用户列表
</div>
<div class="panel-body">
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>号码</th>
<th>价格</th>
<th>级别</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for obj in queryset %}
<tr>
<th>{{ obj.id }}</th>
<th>{{ obj.mobile }}</th>
<th>{{ obj.price }}</th>
<th>{{ obj.get_level_display }}</th>
<th>{{ obj.get_status_display }}</th>
<td>
<a href="/pretty_edit/{{ obj.id }}/" class="btn btn-primary btn-xs">编辑</a>
<a href="/pretty_del/{{ obj.id }}/" class="btn btn-danger btn-xs">删除</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
<nav aria-label="...">
<ul class="pagination">
{# <li class="disabled"><a href="#" aria-label="Previous"><span aria-hidden="true">«</span></a></li>#}
{# <li><a href="?page=1">1 <span class="sr-only">(current)</span></a></li>#}
{# <li><a href="?page=2">2</a></li>#}
{# <li><a href="?page=3">3</a></li>#}
{# <li><a href="?page=4">4</a></li>#}
{# <li><a href="?page=5">5</a></li>#}
{# <li><a href="#" aria-label="Next"><span aria-hidden="true">»</span></a></li>#}
{{ page_string }}
</ul>
<br/>
<form method="GET">
<div class="input-group" style="width: 200px">
<input type="text" name="page" class="form-control" placeholder="页码">
<span class="input-group-btn">
<button class="btn btn-default" type="submit">跳转</button>
</span>
</div>
</form>
</nav>
</div>
{% endblock %}