在web开发中,分页是必不可少的功能,这里通过实例来了解下简单的分页功能。
1.test.py文件
@web_blue.route('/')
def stu_psge():
# 获取page参数 默认为1
page = int(request.args.get('page',1))
# 获取每页显示数据条数默认为2
per_page = int(request.args.get('perpage',2))
# 从数据库查询数据
paginates = Test.query.order_by('id').paginate(page,per_page,error_out=False)
stus = paginates.items
# totalpage为总页面数
totalpage = math.ceil(paginates.total/per_page)
return render_template('test.html',paginate = paginates,stus = stus,totalpage = totalpage)
注意:paginates是flask_sqlalchemy.Pagination类型。
math.celi表示对页面数向上取整,需要引入math模块。
2.test.html文件
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF