分页一(very low)


因为数据量过大,而又想直观便捷的查看数据,进而通过分页显示就可以完成这项工作

app中views.py

LIST=[]                #全局定义一个LIST
for i in range(100): #数据量为100
LIST.append(i) #使LIST里面包含0-99个自然数 def user_list(request):
current_page=request.GET.get('p',1) #用户不存在默认看第一页
current_page=int(current_page) #使char型变为int型
start=(current_page-1)*10 #第一页
end=current_page*10 #最后一页
data=LIST[start:end] #使数据自动分页,每页10个数据 all_count=len(LIST) # 计算LIST的长度
count,y=divmod(all_count,10) #divmod为算法 all_count/10,商为count,余数为y
if y: #y不为0
count+=1 #页数+1
page_list=[]
for i in range(1,count+1):
if i==current_page: #i==页数
temp='<a class="page active" href="/user_list/?p=%s">%s</a>'%(i,i) #点击页数时,通过css使其页数栏变色
else:
temp='<a class="page" href="/user_list/?p=%s">%s</a>'%(i,i)
page_list.append(temp) page_str=''.join(page_list) #以空的字符串将页数连接起来
from django.utils.safestring import mark_safe page_str=mark_safe(page_str) #使page_str为安全 return render(request,'user_list.html',{'li':data,'page_str':page_str})

html文件

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.pagination .page{
display: inline-block;
padding: 5px;
background-color: lavender;
color: black;
margin: 10px;
}
.pagination .page .active{
background-color: red;
}
</style>
</head>
<body>
<ul>
{% for item in li %}
{% include 'li.html' %}
{% endfor %}
</ul>
<div class="pagination">
{{ page_str }}
</div> </body>
</html>

li.html

<li>{{ item }}</li>

分页二(一般low)


LIST = []
for i in range(200):
LIST.append(i) def user_list(request):
current_page = request.GET.get('p', 1) # 用户不存在默认看第一页
current_page = int(current_page)
dighter = 10
start = (current_page - 1) * dighter
end = current_page * dighter
data = LIST[start:end] all_count = len(LIST)
count, y = divmod(all_count, 10)
if y:
count += 1 page_list = []
page_num = 11
if all_count < page_num: #如果总页数小于11页
start_index = 1 #起始页为1
end_index = all_count + 1 #终止页为总页数
else:
if current_page <= (page_num + 1) / 2: #如果当前页小于6页
start_index = 1 #起始页为1
end_index = 11 + 1 #终止页为11页
else:
start_index = current_page - (page_num - 1) / 2 #起始页为当前页-5
end_index = current_page + (page_num - 1) / 2 + 1 #终止页为当前页+5
if (current_page + (page_num - 1) / 2) > all_count: #如果当前页+5大于总页数
start_index = all_count - page_num + 1 #起始页为总页数-11+1
end_index = all_count + 1 #终止页为总页数 if current_page == 1: #如果当前页等于第一页
prev = '<a class="page" href="#">上一页</a>' #则停止向前索引
else:
prev = '<a class="page active" href="/user_list/?p=%s">上一页</a>' % (current_page - 1) #否则可以按上一页向前进行索引
page_list.append(prev) for i in range(int(start_index), int(end_index)): #起始页-终止页数为整数
if i == current_page: #如果i为当前页
temp = '<a class="page active" href="/user_list/?p=%s" >%s</a>' % (i, i) # 将当前的页数序号加上颜色
else:
temp = '<a class="page" href="/user_list/?p=%s">%s</a>' % (i, i) # 没有选定的其他页数不加颜色
page_list.append(temp) if current_page == all_count: #如果当前页为总页数
prev = '<a class="page" href="#">下一页</a>' #停止索引
else:
prev = '<a class="page active" href="/user_list/?p=%s">下一页</a>' % (current_page + 1) #否则可以按下一页向下进行索引
page_list.append(prev) jump = """
<input type="text" /><input type="button" value="确定" onclick="jumpTo(this,'/user_list/?p=')" > #跳转至...页
<script>
function jumpTo(ths,base){ #base为/user_list/?p=
var val=ths.previousSibling.value; #获取text框里的数值
location.href=base + val ; #/user_list/?p= + text框里的内容进行跳转
}
</script> """
page_list.append(jump) page_str = ''.join(page_list)
from django.utils.safestring import mark_safe page_str = mark_safe(page_str) return render(request, 'user_list.html', {'li': data, 'page_str': page_str})

分页三(一般般)


###########将分页二变为面向对象编程#############

class Page:

    def __init__(self,current_page,data_count,per_page_count=10,page_num=11):
self.current_page=current_page
self.data_count=data_count
self.per_page_count=per_page_count
self.page_num=page_num def start(self):
return (self.current_page-1)*self.per_page_count def end(self):
return self.current_page*self.per_page_count @property
def all_count(self):
v = len(LIST)
v, y = divmod(self.data_count, self.per_page_count)
if y:
v += 1
return v def page_str(self,base_url):
page_list = []
if self.all_count < self.page_num:
start_index = 1
end_index = self.all_count + 1
else:
if self.current_page <= (self.page_num + 1) / 2:
start_index = 1
end_index = 11 + 1
else:
start_index = self.current_page - (self.page_num - 1) / 2
end_index = self.current_page + (self.page_num - 1) / 2 + 1
if (self.current_page + (self.page_num - 1) / 2) > self.all_count:
start_index = self.all_count - self.page_num + 1
end_index = self.all_count + 1 if self.current_page == 1:
prev = '<a class="page" href="#">上一页</a>'
else:
prev = '<a class="page active" href="%s?p=%s">上一页</a>' % (base_url,self.current_page - 1)
page_list.append(prev) for i in range(int(start_index), int(end_index)):
if i == self.current_page:
temp = '<a class="page active" href="%s?p=%s" >%s</a>' % (base_url,i, i)
else:
temp = '<a class="page" href="%s?p=%s">%s</a>' % (base_url,i, i)
page_list.append(temp) if self.current_page == self.all_count:
prev = '<a class="page" href="#">下一页</a>'
else:
prev = '<a class="page active" href="%s?p=%s">下一页</a>' % (base_url,self.current_page + 1)
page_list.append(prev) jump = """
<input type="text" /><input type="button" value="确定" onclick="jumpTo(this,'%s?p=')" >
<script>
function jumpTo(ths,base){
var val=ths.previousSibling.value;
location.href=base + val ;
}
</script> """%(base_url)
page_list.append(jump) page_str = ''.join(page_list)
from django.utils.safestring import mark_safe page_str = mark_safe(page_str) return page_str LIST=[]
for i in range(200):
LIST.append(i) def user_list(request):
current_page=request.GET.get('p',1) #用户不存在默认看第一页
current_page=int(current_page)
page_obj=Page(current_page,len(LIST))
data=LIST[page_obj.start():page_obj.end()]
page_str=page_obj.page_str("/user_list/")
return render(request,'user_list.html',{'li':data,'page_str':page_str})

Django中html里的分页显示的更多相关文章

  1. Django中扩展Paginator实现分页

    Reference:https://my.oschina.net/kelvinfang/blog/134342 Django中已经实现了很多功能,基本上只要我们需要的功能,都能够找到相应的包.要在Dj ...

  2. Django中的模板和分页

    模板 在Templates中添加母版: - 母版...html 母版(master.html)中可变化的地方加入: {%block content%}{%endblock%} 在子版 (usermg. ...

  3. java中对list进行分页显示数据到页面

    http://blog.csdn.net/Tang_Mr/article/details/76212184#10006-weixin-1-52626-6b3bffd01fdde4900130bc5a2 ...

  4. Django 中设置分页页码,只显示当前页以及左右两页

    设置后的效果如下: Django 给我们提供了分页的功能:`Paginator`和`Page`类都是用来做分页的.他们在Django中的路径为:`from django.core.paginator ...

  5. Django中的分页,cookies与session

    cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不 ...

  6. Django中的session和cookie及分页设置

    cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不 ...

  7. django中如何实现分页功能

    1.在html页面中导入js文件和css文件 <link rel="stylesheet" href="../../../static/css/jquery.pag ...

  8. Delphi for iOS开发指南(8):在iOS应用程序中使用Tab组件来显示分页

    Delphi for iOS开发指南(8):在iOS应用程序中使用Tab组件来显示分页 在FireMonkey iOS应用程序中的Tab Tab由FMX.TabControl.TTabControl定 ...

  9. django django中的HTML控件及参数传递方法 以及 HTML form 里的数据是怎么被包成http request 的?如何在浏览器里查看到这些数据?

    https://www.jb51.net/article/136738.htm django中的HTML控件及参数传递方法 下面小编就为大家分享一篇django中的HTML控件及参数传递方法,具有很好 ...

随机推荐

  1. 【Web Shell】- 技术剖析中国菜刀 - Part II

    在第一部分,简单描述了中国菜刀的基本功能.本文我将剖析中国菜刀的平台多功能性.传输机制.交互模式和检测.我希望通过我的讲解,您能够根据您的环境检测出并清除它. 平台 那么中国菜刀可以在哪些平台上运行? ...

  2. yii2微博第三方登录

    原作者:杜文建 原博客:http://www.cnblogs.com/dwj97/p/6530568.html yii2微博第三方登录   微博登录是最常用的第三方账号登录之一.由于其网站用户量大,可 ...

  3. CSU1392(NCPC2013)_Number Trick

    给一个小数X,找个A使得:AX=(A循环左移一位) 首先,假设A为一个满足题目条件的数,有n个数位,且最高位数字为A0. 那么可列出方程:AX=(A-A0*10n-1)*10+A0 ————>& ...

  4. 013 BIO、NIO、AIO的区别

    作者:nnngu GitHub:https://github.com/nnngu 博客园:http://www.cnblogs.com/nnngu 简书:https://www.jianshu.com ...

  5. JAVA ACM 基础

    java ACM Java做ACM-ICPC的特点: (1) 在一般比赛中,Java程序会有额外的时间和空间,而实际上经过实验,在执行计算密集任务的时候Java并不比C/C++慢多少,只是IO操作较慢 ...

  6. 【BZOJ3675】【Apio2014】序列分割

    Description 传送门 Solution ​ 之前我也遇到过一次这种"两段之和乘积作为贡献"的问题:考虑把这一种\((\sum) *(\sum)\)的形式拆括号,就可以发现 ...

  7. BZOJ3829 [Poi2014]FarmCraft 【树形dp】

    题目链接 BZOJ3829 题解 设\(f[i]\)为从\(i\)父亲进入\(i\)之前开始计时,\(i\)的子树中最晚装好的时间 同时记\(siz[i]\)为节点\(i\)子树大小的两倍,即为从父亲 ...

  8. 洛谷 P2144 [FJOI2007]轮状病毒

    P2144 [FJOI2007]轮状病毒 题目描述 轮状病毒有很多变种.许多轮状病毒都是由一个轮状基产生.一个\(n\)轮状基由圆环上\(n\)个不同的基原子和圆心的一个核原子构成.\(2\)个原子之 ...

  9. 【模板】exBSGS/Spoj3105 Mod

    [模板]exBSGS/Spoj3105 Mod 题目描述 已知数\(a,p,b\),求满足\(a^x\equiv b \pmod p\)的最小自然数\(x\). 输入输出格式 输入格式: 每个测试文件 ...

  10. 解题:NOI 2016 优秀的拆分

    题面 其实题目不算很难,但是我调试的时候被玄学了,for循环里不写空格会RE,写了才能过.神**调了一个多小时是这么个不知道是什么的玩意(真事,可以问i207M=.=),心态爆炸 发现我们只要找AA或 ...