python django 分页实现

发表时间:2019-10-23 10:25:18
摘要信息

django 分页实现

自己封装一个类

class PageObject:
def __init__(self):
self.data = None

# 分页查询
def handlePage(self, mgsLsit, page, recPerPage):

# 获取当前页码的数据
page_list = mgsLsit.page(page)
# 查询是否有上页下页
try:
next_page = page_list.next_page_number()
except:
next_page = False
try:
pre_page = page_list.previous_page_number()
except:
pre_page = False
rangeList = list(mgsLsit.page_range)
page_range = []
# 分页区间规则
if page > 4 and page < len(rangeList) - 3:
if len(rangeList) > 6:
rangeList = [rangeList[0], False, page - 1, page, page + 1,
False, rangeList[len(rangeList) - 1]]
elif page >= len(rangeList) - 3:
if len(rangeList) > 6:
rangeList = [rangeList[0], False, rangeList[len(rangeList) - 5], rangeList[len(rangeList) - 4],
rangeList[len(rangeList) - 3],
rangeList[len(rangeList) - 2], rangeList[len(rangeList) - 1]]
else:
for item in rangeList:
page_range.append(item)
rangeList = page_range
else:
if len(rangeList) > 6:
rangeList = [rangeList[0], rangeList[1], rangeList[2], rangeList[3], rangeList[4],
False, rangeList[len(rangeList) - 1]]
else:
for item in rangeList:
page_range.append(item)
rangeList = page_range
# 生成分页对象
pageData = {
'rec_per_page': recPerPage,
'now_page': page,
'count': mgsLsit.count,
'num_pages': mgsLsit.num_pages,
'page_range': rangeList,
'has_previous': page_list.has_previous(),
'has_next': page_list.has_next(),
'next_page': next_page,
'pre_page': pre_page,
'page_list': list(page_list)
}
return pageData

使用:

from django.core.paginator import Paginator
from common.page import PageObject
handle = PageObject()
page = 1
todayPY = Paginator(p_message.objects.all().values().order_by('-update_time'),
6)
todayData = handle.handlePage(todayPY, page, 6)

分页html模板

<p class="page-box">
<span>共有{{ list.count }}条记录</span>
<span>当前{{ list.now_page }}/{{ list.num_pages }}页</span>
<span>每页共{{ list.rec_per_page }}条记录</span>
<span>
{% if list.has_previous == False %}
<a class="page-link-a disabled" disabled="disabled">上一页</a>
{% else %}
<a class="page-link-a" href="{{ path }}{{ list.pre_page }}">上一页</a>
{% endif %}
</span>
<span>
{% for item in list.page_range %}
{% if item == False %}
<a class="page-link-a disabled" disabled="disabled"><i
class="icon icon-ellipsis-h"></i></a>
{% else %}
{% if item == list.now_page %}
<a class="page-link-a disabled-active" disabled="disabled">{{ item }}</a>
{% else %}
<a class="page-link-a" href="{{ path }}{{ item }}">{{ item }}</a>
{% endif %}

{% endif %}
{% endfor %}
</span>
<span>
{% if list.has_next == False %}
<a class="page-link-a disabled" disabled="disabled">下一页</a>
{% else %}
<a class="page-link-a" href="{{ path }}{{ list.next_page }}">下一页</a>
{% endif %}
</span>
<span><input type="text" placeholder="页码" id="dumpPage"><button
onclick="dumpPage()" class="btn btn-green">跳转</button></span>
</p>
<script>
let checkNumber = (theObj) => {
var reg = /^[0-9]+.?[0-9]*$/;
if (reg.test(theObj)) {
return true;
}
return false;
}
let dumpPage = () => {
let page = $('#dumpPage').val()
if (page == '{{ list.now_page }}') {
new $.zui.Messager('跳转的页码不能是当前页码!', {
type: 'danger',
placement: 'center'
}).show();
return false;
} else if (!checkNumber(page)) {
new $.zui.Messager('请输入数字的页码!', {
type: 'danger',
placement: 'center'
}).show();
return false;
} else if (page > '{{ list.num_pages }}') {
new $.zui.Messager('跳转的页码不能大于最大页码!', {
type: 'danger',
placement: 'center'
}).show();
return false;
} else if (page == '') {
new $.zui.Messager('请输入页码!', {
type: 'danger',
placement: 'center'
}).show();
return false;
}
window.location.href = '{{ path }}' + page
}
</script>


40% Complete (success)