目录
一、Flask-SQLAlchemy - 直接获取分页后的数据
1-0 基于 flsk-sqlalchemy 的批量数据插入 - add_all(list)
3-1 简单指定当前页page和显示页数per_page获取分页数据
一、Flask-SQLAlchemy - 直接获取分页后的数据
paginate(page=None, per_page=None, error_out=True, max_per_page=None)
Returns per_page items from page page.
If page or per_page are None, they will be retrieved from the request query. If max_per_page is specified, per_page will be limited to that value. If there is no request or they aren’t in the query, they default to 1 and 20 respectively.
如果page或per_page为None,则将从请求查询中检索它们。如果指定了max_per_page,则per_page将限制为该值。如果没有请求或它们不在查询中,则它们分别默认为1和20。
- When error_out is True (default), the following rules will cause a 404 response(当error_out为True(默认值)时,以下规则将导致404响应):
- No items are found and page is not 1. - 找不到任何项目且页面不是1.
- page is less than 1, or per_page is negative. - page小于1,或per_page为负数。
- page or per_page are not ints. - page或per_page不是整数。
- When error_out is False, page and per_page default to 1 and 20 respectively.(当error_out为False时,page和per_page分别默认为1和20。)
Returns a Pagination object. - 返回 Pagination 对象
@app.route('/') @login_required def index(): page=request.args.get('page',1,type=int) pagination=User.query.order_by(User.createTime.desc()).paginate(page,per_page=12,error_out=False) users=pagination.items return render_template('index.html', name=current_user.username,users=users,pagination=pagination)