Flask-Paginate
Flask-Paginate provides simple pagination support for Flask applications, making it easy to display large datasets across multiple pages. It handles page number generation, URL building, and integrates with various CSS frameworks like Bootstrap. The current version is 2024.4.12, following a date-based release cadence with frequent updates for bug fixes and compatibility.
Common errors
-
TypeError: Pagination.__init__() got an unexpected keyword argument 'show_first_page_number'
cause You are using an outdated argument name for including the first page number in the pagination links.fixReplace `show_first_page_number` with `include_first_page_number` in your `Pagination` constructor call. -
ImportError: cannot import name 'Markup' from 'flask'
cause This error occurs when running an older version of `flask-paginate` (prior to 2023.10.8) with Flask 3.0 or newer. Flask 3 moved the `Markup` class from `flask` to `markupsafe`.fixUpgrade `flask-paginate` to version `2023.10.8` or a newer version. `pip install --upgrade flask-paginate`. -
Pagination links or styling appear broken after upgrading `flask-paginate` or `Flask`.
cause The default CSS framework for `flask-paginate` changed from Bootstrap 3 to Bootstrap 4 in version 2021.10.26. Your template HTML might be expecting Bootstrap 3.fixExplicitly set `css_framework='bootstrap3'` in the `Pagination` constructor if you intend to use Bootstrap 3, or update your HTML templates to comply with Bootstrap 4 or 5 styling, then set `css_framework='bootstrap4'` or `css_framework='bootstrap5'` respectively.
Warnings
- breaking The `show_first_page_number` parameter in the `Pagination` constructor was renamed to `include_first_page_number`.
- breaking Older versions of `flask-paginate` (prior to 2023.10.8) are incompatible with Flask 3.0+ due to changes in how `Markup` is imported by Flask.
- gotcha The default CSS framework changed from Bootstrap 3 to Bootstrap 4.
Install
-
pip install flask-paginate
Imports
- Pagination
from flask_paginate import Pagination
- get_page_parameter
from flask_paginate import get_page_parameter
- get_per_page_parameter
from flask_paginate import get_per_page_parameter
Quickstart
import os
from flask import Flask, render_template, request
from flask_paginate import Pagination, get_page_parameter
app = Flask(__name__)
app.secret_key = os.environ.get('FLASK_SECRET_KEY', 'a_secret_key_for_dev')
# Sample data
USERS = [str(i) for i in range(1, 101)]
@app.route('/')
def index():
page = request.args.get(get_page_parameter(), type=int, default=1)
per_page = 10
offset = (page - 1) * per_page
users_for_page = USERS[offset:offset + per_page]
total = len(USERS)
pagination = Pagination(
page=page, per_page=per_page, total=total,
css_framework='bootstrap4',
record_name='users'
)
# In a real app, you would have an 'index.html' template
# For this example, we'll return a minimal string.
# In your template, you would use: {{ pagination.render_macro('pagination.html') }}
return f"""<!doctype html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1, shrink-to-fit=no'>
<link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css' integrity='sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N' crossorigin='anonymous'>
<title>Flask-Paginate Example</title>
</head>
<body>
<div class='container'>
<h1>Users</h1>
<ul>
{''.join([f'<li>User {user}</li>' for user in users_for_page])}
</ul>
<div class='text-center'>
{pagination.render_macro('pagination.html')}
</div>
</div>
</body>
</html>"""
if __name__ == '__main__':
app.run(debug=True)