Python - 自定义分页
2021-02-10 10:19
标签:href 索引 rev text ram 对象 models 判断 数据库 Python - 自定义分页 标签:href 索引 rev text ram 对象 models 判断 数据库 原文地址:https://www.cnblogs.com/zhaoganggang/p/12743627.html"""
使用流程
分页组件应用:
1. 在视图函数中
queryset = models.Issues.objects.filter(project_id=project_id)
page_object = Pagination(
current_page=request.GET.get(‘page‘),
all_count=queryset.count(),
base_url=request.path_info,
query_params=request.GET
)
issues_object_list = queryset[page_object.start:page_object.end]
context = {
‘issues_object_list‘: issues_object_list,
‘page_html‘: page_object.page_html()
}
return render(request, ‘issues.html‘, context)
2. 前端
{% for item in issues_object_list %}
{{item.xxx}}
{% endfor %}
"""
class Pagination(object):
def __init__(self, current_page, all_count, base_url, query_params, per_page=30, pager_page_count=11):
"""
分页初始化
:param current_page: 当前页码
:param per_page: 每页显示数据条数
:param all_count: 数据库中总条数
:param base_url: 基础URL
:param query_params: QueryDict对象,内部含所有当前URL的原条件
:param pager_page_count: 页面上最多显示的页码数量
"""
self.base_url = base_url
try:
self.current_page = int(current_page)
if self.current_page 总页码
if (self.current_page + self.half_pager_page_count) > self.pager_count:
pager_end = self.pager_count
pager_start = self.pager_count - self.pager_page_count + 1
else:
pager_start = self.current_page - self.half_pager_page_count
pager_end = self.current_page + self.half_pager_page_count
page_list = []
if self.current_page 上一页‘
else:
self.query_params[‘page‘] = self.current_page - 1
prev = ‘
上一篇:排序之冒泡排序+对拍器的使用