python-----Django自定义分页器
python-----Django自定义分页器
def page_custom(self, objlist, currentpage=1, p_size=10):
"""
自定义分页器
"""
if not currentpage or int(currentpage) < 1:
currentpage = 1
current_page = int(currentpage)
p_size = int(p_size)
start = (current_page - 1) * p_size
end = current_page * p_size
data = objlist[start:end]
total_count = len(objlist)
total_page = len(objlist) // p_size
remainder = len(objlist) % p_size
if remainder and remainder < p_size:
total_page += 1
res = {
"total_page": total_page,
"page": current_page,
"size": p_size,
"total_count": total_count,
"result": data
}
return res
从小白到大神的蜕变~~