首页 > 代码库 > 简单分页步骤
简单分页步骤
urls:
url(r‘^index/(\d*)‘,views.index)
views:
from django.shoutcuts import render from app impost models from app import common from django.utils.safestring import make_safe def index(request,page): page = common.try_int(page,1) per_item = 5 #每页显示5条 start = (page-1)*per_item end =page-*per_item count = models.Host.objects.all().count() result = models.Host.objects.all()[start:end] sum = -(-count//per_item) #总共多少页 links=[] first_link = ‘<a href =http://www.mamicode.com/‘/index/{}‘>首页<a>‘.format(1) links.append(first_link) prev_link =‘<a href =http://www.mamicode.com/‘/index/{}‘>上一页<a>‘.format(page-1) links.append(prev_link ) for i in range(sum): if page == i+1: link = ‘<a style=‘clour:red;‘ href =http://www.mamicode.com/‘/index/{}‘>{}<a>‘.format(i+1,i+1) else: link = ‘<a href =http://www.mamicode.com/‘/index/{}‘>{}<a>‘.format(i+1,i+1) links.append(link) next_link = ‘<a href =http://www.mamicode.com/‘/index/{}‘>下一页<a>‘.format(page+1) links.append(next_link ) end_link = ‘<a href =http://www.mamicode.com/‘/index/{}‘>尾页<a>‘.format(sum) links.append(end_link) page = make_safe(‘‘.join(links)) #把字符串变成可以显示的代码 ret ={‘count‘:count,‘data‘:result,‘page‘:page} return render(request,‘index.html‘,ret)
common:
def try_int(arg,default): try: arg = int(arg) expect Expection: arg = default return arg
简单分页步骤
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。