目标:每页显示10行,可以上一页,可以下一页,可以首页,末页
方法,每次去查询数据的时候,在url里带上我要查看的页码
第一步:先实现给一个数字就让显示第几页
1:0,10
2:10,20
3:20,30
如上所示,所以只需要传递一个整形,然后起始页为(x-1)10,末页为 x10
如果没有,默认查看第一页。
def classes(request):
if request.method == "GET":
username = request.session.get("username")
# models.Classes.objects.create(caption = "Python一班")
# models.Classes.objects.create(caption = "Python二班")
# for i in range(0,1000):
# models.Classes.objects.create(caption="Python班-%s"%i)
# i+=1
curret_page = int(request.GET.get("page",1))#1表示没有输入的时候默认为1,其实传输过来的是字符串,需要将其转换为整型
print(curret_page)
start_list = (curret_page - 1) * 10
end_list = curret_page * 10
class_list = models.Classes.objects.all()[start_list : end_list]
return render(request, "classes.html",{"username": username,"class_list":class_list})
第二步:在表格下方显示页码
2.1 前台如何打印页码
显示页码需要使用到标签,即在表格下方配置标签
</table>
<div class="page_turnning">
<a href="/classes/classes.html?page=1">1</a>
<a href="/classes/classes.html?page=2">2</a>
<a href="/classes/classes.html?page=3">3</a>
<a href="/classes/classes.html?page=4">4</a>
<a href="/classes/classes.html?page=5">5</a>
</div>
如上,一定要在
后,因为要显示在表格的下方2.2 将这个页码放在后台
</table>
<div class="page_turnning">
<!-- <a href="/classes/classes.html?page=1">1</a>-->
<!-- <a href="/classes/classes.html?page=2">2</a>-->
<!-- <a href="/classes/classes.html?page=3">3</a>-->
<!-- <a href="/classes/classes.html?page=4">4</a>-->
<!-- <a href="/classes/classes.html?page=5">5</a>-->
{{ page_list }}
</div>
先将html中的页码注释掉,随后放置一个模板,用于后台传递过来的数据进行替换
def classes(request):
if request.method == "GET":
username = request.session.get("username")
# models.Classes.objects.create(caption = "Python一班")
# models.Classes.objects.create(caption = "Python二班")
# for i in range(0,1000):
# models.Classes.objects.create(caption="Python班-%s"%i)
# i+=1
curret_page = int(request.GET.get("page",1))#1表示没有输入的时候默认为1
print(curret_page)
start_list = (curret_page - 1) * 10
end_list = curret_page * 10
page_list = """
<a href="/classes/classes.html?page=1">1</a>
<a href="/classes/classes.html?page=2">2</a>
<a href="/classes/classes.html?page=3">3</a>
<a href="/classes/classes.html?page=4">4</a>
<a href="/classes/classes.html?page=5">5</a>
"""
class_list = models.Classes.objects.all()[start_list : end_list]
return render(request, "classes.html",{"username": username,"class_list":class_list,"page_list":page_list})
如下所示,显示不安全。
标记后台代码安全
def classes(request):
if request.method == "GET":
username = request.session.get("username")
# models.Classes.objects.create(caption = "Python一班")
# models.Classes.objects.create(caption = "Python二班")
# for i in range(0,1000):
# models.Classes.objects.create(caption="Python班-%s"%i)
# i+=1
curret_page = int(request.GET.get("page",1))#1表示没有输入的时候默认为1
print(curret_page)
start_list = (curret_page - 1) * 10
end_list = curret_page * 10
page_list = """
<a href="/classes/classes.html?page=1">1</a>
<a href="/classes/classes.html?page=2">2</a>
<a href="/classes/classes.html?page=3">3</a>
<a href="/classes/classes.html?page=4">4</a>
<a href="/classes/classes.html?page=5">5</a>
"""
class_list = models.Classes.objects.all()[start_list : end_list]
from django.utils.safestring import mark_safe
return render(request, "classes.html",{"username": username,"class_list":class_list,"page_list":mark_safe(page_list)})
注意:“from django.utils.safestring import mark_safe” 导入一个模块,然后在“page_list”加上这个函数,这样在前面就可以显示安全,即可展示页面,另外一种方法,即使用前面说的“过滤器” {{ page_list|safe }},这样也可以正常显示
另外一种方法使用过滤器,标识 为safe.
截至现在,页码已经放到后台了。
使用divmod() 根据余数自动算出总页数
def classes(request):
if request.method == "GET":
curret_page = int(request.GET.get("page",1))#1表示没有输入的时候默认为1
print(curret_page)
start_list = (curret_page - 1) * 10
end_list = curret_page * 10
column_account = models.Classes.objects.all().count()
quotients,remainders = divmod(column_account,10)
if remainders == 0:
page_total = quotients
else:
page_total = quotients + 1
page_list = []
for i in range(1,page_total):
page_list.append('<a href="/classes/classes.html?page=%s">%s</a>'%(i,i))
page_list = "".join(page_list)
class_list = models.Classes.objects.all()[start_list : end_list]
from django.utils.safestring import mark_safe
return render(request, "classes.html",{"username": username,"class_list":class_list,"page_list":mark_safe(page_list)})
页码间添加上空格
<style>
.page_turnning a{
display: inline-block;
padding: 5px;
}
</style>
解决上一页,下一页,首页与末页问题
def classes(request):
if request.method == "GET":
curret_page = int(request.GET.get("page",1))#1表示没有输入的时候默认为1
print(curret_page)
start_list = (curret_page - 1) * 10
end_list = curret_page * 10
column_account = models.Classes.objects.all().count()
quotients,remainders = divmod(column_account,10)
if remainders == 0:
page_total = quotients
else:
page_total = quotients + 1
page_list = []
############################
page_list.append('<a href="/classes/classes.html?page=1">首页</a>')
page_list.append('<a href="/classes/classes.html?page=%s">上一页</a>'%(curret_page -1))
for i in range(1,page_total + 1):
page_list.append('<a href="/classes/classes.html?page=%s">%s</a>'%(i,i))
page_list.append('<a href="/classes/classes.html?page=%s">下一页</a>'%(curret_page + 1))
page_list.append('<a href="/classes/classes.html?page=%s">末页</a>'%page_total)
################################
page_list = "".join(page_list)
class_list = models.Classes.objects.all()[start_list : end_list]
from django.utils.safestring import mark_safe
return render(request, "classes.html",{"username": username,"class_list":class_list,"page_list":mark_safe(page_list)})
注意注释符中间一段内容
首页尾页也是一样的处理。
当前页加上样式
def classes(request):
if request.method == "GET":
username = request.session.get("username")
# models.Classes.objects.create(caption = "Python一班")
# models.Classes.objects.create(caption = "Python二班")
# for i in range(0,1000):
# models.Classes.objects.create(caption="Python班-%s"%i)
# i+=1
curret_page = int(request.GET.get("page",1))#1表示没有输入的时候默认为1
print(curret_page)
start_list = (curret_page - 1) * 10
end_list = curret_page * 10
column_account = models.Classes.objects.all().count()
quotients,remainders = divmod(column_account,10)
if remainders == 0:
page_total = quotients
else:
page_total = quotients + 1
page_list = []
page_list.append('<a href="/classes/classes.html?page=1">首页</a>')
page_list.append('<a href="/classes/classes.html?page=%s">上一页</a>'%(curret_page -1))
############################################
for i in range(1,page_total + 1):
if i == curret_page:
page_list.append('<a class = "active" href="/classes/classes.html?page=%s">%s</a>'%(i,i))
else:
page_list.append('<a href="/classes/classes.html?page=%s">%s</a>' % (i, i))
############################################
page_list.append('<a href="/classes/classes.html?page=%s">下一页</a>'%(curret_page + 1))
page_list.append('<a href="/classes/classes.html?page=%s">末页</a>'%page_total)
page_list = "".join(page_list)
class_list = models.Classes.objects.all()[start_list : end_list]
from django.utils.safestring import mark_safe
return render(request, "classes.html",{"username": username,"class_list":class_list,"page_list":mark_safe(page_list)})
注意注释符中间的内容
html加上样式
<style>
.page_turnning a.active{
background-color: coral;
color: white;
}
</style>
只保留11页(显示当前页的前面5页和后面5页)
显示当前页前面5个,后面5个
但是这里有这样一个问题。
如果页数比较少的情况也会有问题。
def classes(request):
if request.method == "GET":
curret_page = int(request.GET.get("page",1))#1表示没有输入的时候默认为1
print(curret_page)
column_account = models.Classes.objects.all().count()
quotients,remainders = divmod(column_account,10)
if remainders == 0:
page_total = quotients
else:
page_total = quotients + 1
page_list = []
page_list.append('<a href="/classes/classes.html?page=1">首页</a>')
page_list.append('<a href="/classes/classes.html?page=%s">上一页</a>'%(curret_page -1))
##############################################################
if page_total < 11:
start_list = 1
end_list = page_total
else:
if curret_page < 6:
start_list = 1
end_list = 11
else:
start_list = curret_page - 5
end_list = curret_page + 5 + 1
if end_list > page_total:
start_list = page_total - 10
end_list = page_total + 1
for i in range(start_list,end_list):
if i == curret_page:
page_list.append('<a class = "active" href="/classes/classes.html?page=%s">%s</a>'%(i,i))
else:
page_list.append('<a href="/classes/classes.html?page=%s">%s</a>' % (i, i))
##############################################################
page_list.append('<a href="/classes/classes.html?page=%s">下一页</a>'%(curret_page + 1))
page_list.append('<a href="/classes/classes.html?page=%s">末页</a>'%page_total)
page_list = "".join(page_list)
class_list = models.Classes.objects.all()[start_list : end_list]
from django.utils.safestring import mark_safe
return render(request, "classes.html",{"username": username,"class_list":class_list,"page_list":mark_safe(page_list)})
注意注释符内的内容
当前页为1的时候,上一页和首页就不显示了。最后一页的时候下一页和末页不显示
def classes(request):
if request.method == "GET":
username = request.session.get("username")
# models.Classes.objects.create(caption = "Python一班")
# models.Classes.objects.create(caption = "Python二班")
# for i in range(0,1000):
# models.Classes.objects.create(caption="Python班-%s"%i)
# i+=1
curret_page = int(request.GET.get("page",1))#1表示没有输入的时候默认为1
print(curret_page)
column_account = models.Classes.objects.all().count()
quotients,remainders = divmod(column_account,10)
if remainders == 0:
page_total = quotients
else:
page_total = quotients + 1
page_list = []
###############################################
if curret_page !=1:
page_list.append('<a href="/classes/classes.html?page=1">首页</a>')
page_list.append('<a href="/classes/classes.html?page=%s">上一页</a>'%(curret_page -1))
###############################################
if page_total < 11:
start_list = 1
end_list = page_total
else:
if curret_page < 6:
start_list = 1
end_list = 11
else:
start_list = curret_page - 5
end_list = curret_page + 5 + 1
if end_list > page_total:
start_list = page_total - 10
end_list = page_total + 1
for i in range(start_list,end_list):
if i == curret_page:
page_list.append('<a class = "active" href="/classes/classes.html?page=%s">%s</a>'%(i,i))
else:
page_list.append('<a href="/classes/classes.html?page=%s">%s</a>' % (i, i))
########################################################
if curret_page != page_total:
page_list.append('<a href="/classes/classes.html?page=%s">下一页</a>'%(curret_page + 1))
page_list.append('<a href="/classes/classes.html?page=%s">末页</a>'%page_total)
########################################################
page_list = "".join(page_list)
class_list = models.Classes.objects.all()[start_list : end_list]
from django.utils.safestring import mark_safe
return render(request, "classes.html",{"username": username,"class_list":class_list,"page_list":mark_safe(page_list)})
注意上图中注释符内的两个if…else
上一页,下一页,首页,末页按钮化
def classes(request):
if request.method == "GET":
username = request.session.get("username")
# models.Classes.objects.create(caption = "Python一班")
# models.Classes.objects.create(caption = "Python二班")
# for i in range(0,1000):
# models.Classes.objects.create(caption="Python班-%s"%i)
# i+=1
curret_page = int(request.GET.get("page",1))#1表示没有输入的时候默认为1
print(curret_page)
column_account = models.Classes.objects.all().count()
quotients,remainders = divmod(column_account,10)
if remainders == 0:
page_total = quotients
else:
page_total = quotients + 1
page_list = []
###############################################
if curret_page !=1:
page_list.append('<a href="/classes/classes.html?page=1"><input type="button" value="首页"></a>')
page_list.append('<a href="/classes/classes.html?page=%s"><input type="button" value="上一页"></a>'%(curret_page -1))
###############################################
if page_total < 11:
start_list = 1
end_list = page_total
else:
if curret_page < 6:
start_list = 1
end_list = 11
else:
start_list = curret_page - 5
end_list = curret_page + 5 + 1
if end_list > page_total:
start_list = page_total - 10
end_list = page_total + 1
for i in range(start_list,end_list):
if i == curret_page:
page_list.append('<a class = "active" href="/classes/classes.html?page=%s">%s</a>'%(i,i))
else:
page_list.append('<a href="/classes/classes.html?page=%s">%s</a>' % (i, i))
###############################################
if curret_page != page_total:
page_list.append('<a href="/classes/classes.html?page=%s"><input type="button" value="下一页"></a>'%(curret_page + 1))
page_list.append('<a href="/classes/classes.html?page=%s"><input type="button" value="末页"></a>'%page_total)
###############################################
page_list = "".join(page_list)
class_list = models.Classes.objects.all()[start_list : end_list]
from django.utils.safestring import mark_safe
return render(request, "classes.html",{"username": username,"class_list":class_list,"page_list":mark_safe(page_list)})
注意如上图注释符内的内容,a标签内添加了input标签
添加一个自己输入页码的框
def classes(request):
if request.method == "GET":
username = request.session.get("username")
# models.Classes.objects.create(caption = "Python一班")
# models.Classes.objects.create(caption = "Python二班")
# for i in range(0,1000):
# models.Classes.objects.create(caption="Python班-%s"%i)
# i+=1
curret_page = int(request.GET.get("page",1))#1表示没有输入的时候默认为1
print(curret_page)
column_account = models.Classes.objects.all().count()
quotients,remainders = divmod(column_account,10)
if remainders == 0:
page_total = quotients
else:
page_total = quotients + 1
page_list = []
if curret_page !=1:
page_list.append('<a href="/classes/classes.html?page=1"><input type="button" value="首页"></a>')
page_list.append('<a href="/classes/classes.html?page=%s"><input type="button" value="上一页"></a>'%(curret_page -1))
if page_total < 11:
start_list = 1
end_list = page_total
else:
if curret_page < 6:
start_list = 1
end_list = 11
else:
start_list = curret_page - 5
end_list = curret_page + 5 + 1
if end_list > page_total:
start_list = page_total - 10
end_list = page_total + 1
for i in range(start_list,end_list):
if i == curret_page:
page_list.append('<a class = "active" href="/classes/classes.html?page=%s">%s</a>'%(i,i))
else:
page_list.append('<a href="/classes/classes.html?page=%s">%s</a>' % (i, i))
if curret_page != page_total:
page_list.append('<a href="/classes/classes.html?page=%s"><input type="button" value="下一页"></a>'%(curret_page + 1))
page_list.append('<a href="/classes/classes.html?page=%s"><input type="button" value="末页"></a>'%page_total)
########################################################
page_list.append(' <form style="display: inline-block" action="/classes/classes.html" method="get"><input type="number" min="1" size="5" name="page" placeholder="1-%s" ><input type="submit" value="跳转"></form>'%page_total)
###############################################
page_list = "".join(page_list)
class_list = models.Classes.objects.all()[start_list : end_list]
from django.utils.safestring import mark_safe
return render(request, "classes.html",{"username": username,"class_list":class_list,"page_list":mark_safe(page_list)})
注意注释符内的内容
总结:翻页代码封装类(可复用)
目标:定义一个类,形参3个,总条目数,当前页,跳转的url,显示几页,然后类里面自动生成页码块![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/40673550bd5f442c4eb2d4a6bd03af29.png)
新建一个python 包,里面新建一个python文件,名称叫pagenation.py
python 定义内容
# -*- coding: utf-8 -*-
# @Time : 2022/5/17 11:13
# @FileName: pagenation.py
# @Software: PyCharm
class pagenation:
def __init__(self,db_account,current_page,redirect_url,num_of_page=10):
"""
db_account: 传参总共的数据的行数,可以从数据库读取
current_page:当前的页面,类的形参从django后端获取,而后端从前端的html界面获取
redirect_url: 前端点击页面后跳转的url
num_of_page: 可以翻多少页,如可以显示10页,默认是10,django后端views中可以更改
"""
self.db_account = db_account
self.current_page = current_page
self.redirect_url = redirect_url
self.num_of_page = num_of_page
@property
def page_start(self):
return ((self.current_page -1) * self.num_of_page )
@property
def page_end(self):
return (self.current_page * self.num_of_page )
def page_total(self):
quotients,remainders = divmod(self.db_account,self.num_of_page)
if remainders == 0:
page_total = quotients
else:
page_total = quotients + 1
return page_total
def page_html(self):
page_list = []
value = self.page_total()
if self.current_page != 1:
page_list.append('<a href="%s"><input type="button" value="首页"></a>'% self.redirect_url)
page_list.append(
'<a href="%s?page=%s"><input type="button" value="上一页"></a>' % (self.redirect_url,self.current_page - 1))
if value < self.num_of_page + 1:
start_list = 1
end_list = value
else:
if self.current_page < 6:
start_list = 1
end_list = self.num_of_page + 1
else:
start_list = self.current_page - round(self.num_of_page/2)
end_list = self.current_page + round(self.num_of_page/2) + 1
if end_list > value:
start_list = value - self.num_of_page + 1
end_list = value + 1
for i in range(start_list, end_list):
if i == self.current_page:
page_list.append('<a class = "active" href="%s?page=%s">%s</a>' % (self.redirect_url,i, i))
else:
page_list.append('<a href="%s?page=%s">%s</a>' % (self.redirect_url,i, i))
if self.current_page != value:
page_list.append(
'<a href="%s?page=%s"><input type="button" value="下一页"></a>' % (self.redirect_url,self.current_page + 1))
page_list.append(
'<a href="%s?page=%s"><input type="button" value="末页"></a>' % (self.redirect_url,value))
page_list.append(
'<form style="display: inline-block" action="%s" method="get"><input type="number" min="1" size="5" name="page" placeholder="1-%s" ><input type="submit" value="跳转"></form>' % (self.redirect_url,value))
page_list = "".join(page_list)
return page_list
修改views
@auth
def classes(request):
if request.method == "GET":
username = request.session.get("username")
curret_page = int(request.GET.get("page",1))#1表示没有输入的时候默认为1
from utils.pagenation import pagenation
column_account = models.Classes.objects.all().count()
obj = pagenation(column_account,curret_page,"/classes/classes.html",rows_of_page=10,pages_num=10)
page_list = obj.page_html()
class_list = models.Classes.objects.all()[obj.page_start : obj.page_end]
from django.utils.safestring import mark_safe
return render(request, "classes.html",{"username": username,"class_list":class_list,"page_list":mark_safe(page_list)})