Pagination (Flask-SQLAlchemy)
Pagination splits a large result set into pages so you fetch and display only a slice at a time. Flask-SQLAlchemy provides a paginate() method that returns a page of items plus navigation info like total pages and next/prev.
Flask
Showing thousands of rows at once is slow and overwhelming. Pagination queries just one page's worth of rows (an OFFSET/LIMIT under the hood) and gives you metadata to build 'Previous / Next' links.
With Flask-SQLAlchemy you call Model.query.paginate(page=page, per_page=20). The returned Pagination object exposes .items (this page's rows), .page, .pages (total page count), .has_next, .has_prev, and .iter_pages() for rendering a page-number list.
You typically read the requested page from the query string: page = request.args.get('page', 1, type=int). Combine pagination with order_by so the slices are stable across requests.
Syntax
Model.query.paginate(page=1, per_page=20)Examples
Python
@app.route("/posts")
def posts():
page = request.args.get("page", 1, type=int)
pg = Post.query.order_by(Post.id).paginate(page=page, per_page=10)
return render_template("posts.html", posts=pg.items, pagination=pg)