一百四十:CMS系统之使用flask-paginate实现分页功能
官方文档:https://pythonhosted.org/Flask-paginate/
安装:pip install flask-paginate
在没有分页的情况下,默认会加载所有内容
在config中配置每页显示的条数
使用
from flask_paginate import Pagination, get_page_parameter
bp = Blueprint("front", __name__)
@bp.route('/')
def index():
banners = BannerModel.query.order_by(BannerModel.priority.desc()).limit(4) # 只取4条
boards = BoardModel.query.all()
page = request.args.get(get_page_parameter(), type=int, default=1)
start = (page - 1) * config.PER_PAGE
end = start + config.PER_PAGE
posts = PostModel.query.slice(start, end)
# bs_version: bootstrap版本
pagination = Pagination(bs_version=3, page=page, total=PostModel.query.count())
context = {'banners': banners, 'boards': boards, 'posts': posts, 'pagination': pagination}
return render_template('front/front_index.html', **context)
实现翻页功能
效果
按钮居中
讨论群:249728408