python的Web框架,html分页

时间:2021-09-14 03:36:09

使用简单的算法得出页码数,然后在html中获取即可。仅供参考。

views的写法

 def crm_stu(request):
section = '教师后台管理页'
search = request.GET.get('search', '').strip() #搜索的值
if search:
# 如果是数字,则返回qq或者phone的查询结果
if search.isdigit():
sts = Students.objects.filter(Q(qq=search) | Q(phone=search), is_deleted=False).order_by('-e_time')
# 如果不是数字,则返回姓名的查询结果。
else:
sts = Students.objects.filter(name__contains=search, is_deleted=False).order_by('-e_time') # 如果搜索的值不存在,则返回没有删除的值。
else:
sts = Students.objects.filter(is_deleted=False).order_by('-e_time') 把需要的数据计算出来,然后传给tags标签
# 当前页
page = request.GET.get('page', 1)
page = int(page)
# 每页显示的数据条数
per_page = request.GET.get('per_page', 10)
per_page = int(per_page)
# 总学生数
total_count = sts.count()
# 总页数
total_page = math.ceil(total_count/per_page) # 获取数据的范围。
sts = sts[(page-1)*per_page:page*per_page] #把需要的参数和值传送给html
return render(request, 'teacher/crm-stu.html', context={
'section': section,
'sts': sts,
'search': search,
'page': page,
'per_page': per_page,
'total_page':total_page,
})

标签tags的方法定义

 # 注册并把配置的网页放进来,并把context传给网页
@register.inclusion_tag('inclu_tags/pagination.html',takes_context=True) def pagination(context): # 除当前页外,每两边的页码数
num = 2
page = context['page']
per_page = context['per_page']
total_page = context['total_page'] page_list = []
# 生成左边的页码以及当前页码
if page - num <= 0: #如果当前页面减去num的数量小于等于0,则左边页码是展示不全的,不够num的数量
for i in range(page):
page_list.append(i+1)
else:
for i in range(page-num,page+1):
page_list.append(i) # 生成右边的页码
if page + num >= total_page: #如果当前的页码加上num的数量大于总页数,则右边的页码超出了总页数,就需要页码只到总页数为止
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) return {
'page_list': page_list,
'context': context,
}

num代表页面两边的页面数量。每边的页码只有2个,则num就是2。

python的Web框架,html分页

html中的用法:css样式请忽略

 <nav aria-label="Page navigation">
<ul class="pagination" style="margin: 0"> <!--如果当前页码为1,则显示css样式,不能点击-->
<li {% if context.page == 1 %}class="disabled" {% endif %}>
如果当前页码为大于1,则加上href标签,可以有点击上一页的标签,且上一页的标签需要渲染出来。是当前网址+参数(搜索内容,当前页码-1,每页展示的数据条数)
<a {% if context.page > 1 %}href="{{ request.path }}?search={{ context.search }}&page={{ context.page|add:'-1' }}
&per_page={{ context.per_page }}"{% endif %} aria-label="Next">
<span aria-hidden="true">&laquo;</span>
</a> </li>
{% for page in page_list %}
<li {% if context.page == page %}class="active" {% endif %}><a href="{{ request.path }}?search={{ context.search }}
&page={{ page }}&per_page={{ context.per_page }}">{{ page }}</a></li>
{% endfor %}
<li {% if context.page == context.total_page %}class="disabled" {% endif %}>
<a {% if context.page < context.total_page %}href="{{ request.path }}?search={{ context.search }}&page=
{{ context.page|add:'1' }}&per_page={{ context.per_page }}"{% endif %} aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
<li><a href="{{ request.path }}?page=1&per_page={{ context.per_page }}">回到首页</a></li>
</ul>
</nav>

Django内置分页功能

# 接受两个参数
from django.core.paginator import Paginator
from teacher.models import Students
>>>p = Paginator(Students.objects.all().ordet_by('-c_time'), 3) # 总数据量
>>> p.count
15 # 总页数
>>> p.nun_pages
5 # 页面范围
>>> p.page_range
range(1,6) # 返回第一页的数据
>>> page1 = p.page(1) # 获取的是一个可迭代的set字段
>>> page1.object_list
<QuerySet [<Students: 哈哈哈-23>, <Students: 哈哈-15>, <Students: aaaasdds-16>]> # 是否有上一页
>>> page1.has_previous()
False # 是否有下一页
>>> page.has_next()
True # 下一页的页码
>>> page1.next_page_number()
2 # 获取当前页的数据
>>> s = p.get_page(1) # 当前页码
>>>page1.nunber
1