prettynum_list.html
{% extends 'layout.html' %}
{% block content %}
<div class="container">
<div style="margin-bottom: 10px" class="clearfix">
<a class="btn btn-success" href="/prettynum/add/">
<span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span>
新建靓号
</a>
<div style="float: right; width: 300px;">
<form>
<div class="input-group">
<input type="text" name="q" class="form-control" placeholder="Search for..."
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">
<!-- Default panel contents -->
<div class="panel-heading">
<span class="glyphicon glyphicon-th-list" aria-hidden="true"></span>
靓号列表
</div>
<!-- Table -->
<table class="table table-bordered">
<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>
<td>{{ obj.mobile }}</td>
<td>{{ obj.price }}</td>
<td>{{ obj.get_level_display }}</td>
<td>{{ obj.get_status_display }}</td>
<td>
<a class="btn btn-primary btn-xs" href="/prettynum/{{ obj.id }}/edit/">编辑</a>
<a class="btn btn-danger btn-xs" href="/prettynum/{{ obj.id }}/delete/">删除</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="clearfix">
<ul class="pagination" style="float: left;">
{{ page_string }}
<li>
<form style="float: left; margin-left: -1px" method="get">
<input type="text" name="page"
style="position: relative; float: left; display: inline-block; width: 80px; border-radius: 0;"
class="form-control" placeholder="页码">
<button style="border-radius: 0" class="btn btn-default" type="submit">跳转</button>
</form>
</li>
</ul>
</div>
</div>
{% endblock %}
将<li>...</li>剪切写在后台
<li>
<form style="float: left; margin-left: -1px" method="get">
<input type="text" name="page"
style="position: relative; float: left; display: inline-block; width: 80px; border-radius: 0;"
class="form-control" placeholder="页码">
<button style="border-radius: 0" class="btn btn-default" type="submit">跳转</button>
</form>
</li>
views.py
def prettynum_list(request):
"""靓号列表"""
#循环创建300条数据
# for i in range(300):
# models.PrettyNum.objects.create(mobile="15002609022", price=10, level=1, status=1)
# # http://127.0.0.1:8000/prettynum/list/?q=0710浏览器传入参数
# value = request.GET.get('q')
# #筛选出mobile中包含0710的手机号
# data_dict = {"mobile__contains": "0710"}
# res = models.PrettyNum.objects.filter(**data_dict)
# print(res)
data_dict = {}
search_data = request.GET.get('q', "")
if search_data:
data_dict[ "mobile__contains"] = search_data
# res = models.PrettyNum.objects.filter(**data_dict)
# print(res)
#获取当前访问的页码,根据用户想要访问的页码,计算出起止位置
page = int(request.GET.get('page', 1))
page_size = 10
start = (page-1) * page_size
end = page * page_size
# 数据中获取所有数据
# order_by("-level")以级别倒序排列
#取到所有的数据
# queryset = models.PrettyNum.objects.filter(**data_dict).order_by("-level")
# 取到前10条的数据
# queryset = models.PrettyNum.objects.filter(**data_dict).order_by("-level")[0:10]
# http://127.0.0.1:8000/prettynum/list/?page=3浏览器传参
queryset = models.PrettyNum.objects.filter(**data_dict).order_by("-level")[start:end]
#获取到符合条件的数据的总条数
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
if total_page_count <= 2 * plus + 1:
#数据库中的数据比较少,都没有达到11项
start_page = 1
end_page = total_page_count
else:
#数据库中的数据比较多 大于11项
#当前页小于5时, (小极值)
if page <= plus:
start_page = 1
end_page = 2 * plus + 1
else:
#当前页>5
#当前页+5>总页面
if (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
#页码
page_str_list = []
#首页
page_str_list.append('<li><a href="/prettynum/list/?page={}">首页</a></li>'.format(1))
#上一页
if page > 1:
prev = '<li><a href="/prettynum/list/?page={}">上一页</a></li>'.format(page - 1)
else:
prev = '<li><a href="/prettynum/list/?page={}">上一页</a></li>'.format(1)
page_str_list.append(prev)
#页面
#range前取后不取,所以total_page_count+1
for i in range(start_page, end_page + 1):
#给当前页加一个样式
if i == page:
ele = '<li class="active"><a href="/prettynum/list/?page={}">{}</a></li>'.format(i, i)
else:
ele = '<li><a href="/prettynum/list/?page={}">{}</a></li>'.format(i, i)
page_str_list.append(ele)
#下一页
if page < total_page_count:
prev = '<li><a href="/prettynum/list/?page={}">下一页</a></li>'.format(page + 1)
else:
prev = '<li><a href="/prettynum/list/?page={}">下一页</a></li>'.format(total_page_count)
page_str_list.append(prev)
#尾页
page_str_list.append('<li><a href="/prettynum/list/?page={}">尾页</a></li>'.format(total_page_count))
search_string = """
<li>
<form style="float: left; margin-left: -1px" method="get">
<input type="text" name="page"
style="position: relative; float: left; display: inline-block; width: 80px; border-radius: 0;"
class="form-control" placeholder="页码">
<button style="border-radius: 0" class="btn btn-default" type="submit">跳转</button>
</form>
</li>
"""
page_str_list.append(search_string)
page_string = mark_safe("".join(page_str_list))
return render(request, 'prettynum_list.html', {'queryset': queryset, "search_data": search_data, "page_string": page_string})
标签:count,list,点击,dict,跳转,后台,total,data,page From: https://www.cnblogs.com/kekeeleven/p/17771374.html