分页例子程序:
分页函数:
LIST = [] ???#全局列表for i in range(100): ?#1:100的列表 ???LIST.append(i)def user_list(request): ???current_page = request.GET.get(‘p‘, 1) ?# GET接收的都是字符串 ???current_page = int(current_page) ???#字符串转换成数字
per_page_count = 10 ???????#每页显示多少条数据 ???start = (current_page - 1) * per_page_count ?????#页数显示数据开始 ???end = current_page * per_page_count ????????????#页数显示数据接收 ???data = LIST[start:end] ?????????????#生成显示列表的索引 ???all_count = len(LIST) ??????????????#判断列表总长度 ???count, y = divmod(all_count, per_page_count) ??????#取模函数,取10的模,count为取模多少次,y是剩余多少 ???if y: ??????????????????????????#如果y不为0 说明还有剩余的数 ???????count += 1 ???page_list = [] ?????????????#建立一个空列表 ???for i in range(1, count+1): ????#显示的页数是取模的数 ???????if i == current_page: ??????#如果判断页数为当前显示的页数 ???????????temp = ‘<a class="page active" href="/user_list/?p=%s">%s</a>‘ % (i, i)
#给列表中传这个字符串,class中加入active的css样式 ???????else: ???????????temp = ‘<a class="page" href="/user_list/?p=%s">%s</a>‘ % (i, i) #不是当前显示就传这个字符串 ???????page_list.append(temp) ?????#将字符串压入列表 ???page_str = mark_safe("".join(page_list)) ???#转换成字符串,并打上安全标记传都前端 ???return render(request, ‘user_list.html‘, {‘li‘: data, ‘page_str‘: page_str})
<!DOCTYPE html><html lang="en"><head> ???<meta charset="UTF-8"> ???<title>Title</title> ???<style> ???????.pagination .page{ ????????????????#页数标签演示 ???????????display: inline-block; ???????????padding: 5px; ???????????background-color: cornflowerblue; ???????????margin: 10px; ???????} ???????.pagination .page.active{ ??#当前标签的样式 ???????????background-color: #00aa00; ???????????color: white; ???????} ???</style></head><body> ???<ul> ???????{% for item in li %} ???????????{% include ‘li.html‘ %} ???#引入tag模板标签 ???????{% endfor %} ???</ul> ???<div class="pagination"> ???????{{ page_str }} #接收到html字符标签 ???</div></body></html>
Django-website 程序案例系列-8 分页
原文地址:http://www.cnblogs.com/kuku0223/p/7906257.html