Django学习手册 - 自定义分页函数

前端代码:

   <div class="xianshi">
{% for i in info %}
<ul>
<li>{{ i }}</li>
</ul>
{% endfor %}
</div> <div class="yema">
{% for x in yemalist%}
<span>{{ x | safe}}</span>
{% endfor%}
</div>

分页函数代码:

def text(yema, count, data,path):
"""
:param yema: 页码,web前端传递的页码值
:param count: 每页显示数值
:param data: 数据库中读取的数据
:param path: 当前分页页面url路径
:return: 字典数据
""" yema = int(yema)
path1 = path.split('/')[1] start = count * (yema - 1)
end = count * yema
info = data[start:end] if divmod(len(data), count)[1] != 0:
s = divmod(len(data), count)[0] + 2
else:
s = divmod(len(data), count)[0] + 1 yema_list = []
for i in range(1, s):
i2 = "<a href='/%s/%s'>%s</a>" % (path1,i, i)
yema_list.append(i2) if yema<=5:
xy = "<a href='/%s/%s'>下一页</a>" % (path1,yema + 1)
yema_list = yema_list[0:10]
yema_list.append(xy)
elif 6<=yema<=(s-2):
sy = "<a href='/%s/%s'>上一页</a>"% (path1,yema-1)
xy = "<a href='/%s/%s'>下一页</a>" % (path1,yema + 1)
yema_list = yema_list[yema-5:yema+5]
yema_list.insert(0,sy)
yema_list.append(xy)
elif yema==(s-1):
sy = "<a href='/%s/%s'>上一页</a>" % (path1,1)
yema_list = yema_list[-10:-1]
yema_list.insert(0, sy) return {'info': info, "yemalist":yema_list}

后台代码:引入函数,赋值,返回参数即可

Django学习手册 - 自定义分页函数

测试:

Django学习手册 - 自定义分页函数

上一篇:JS的包装类


下一篇:C# 基础知识系列- 17 实战篇 编写一个小工具(1)