| 1 |
Paging Query Results |
| 2 |
==================== |
| 3 |
|
| 4 |
If you have a lot of results, you may only want to show a certain number at a time, |
| 5 |
allowing the user to click next and previous links to see pages of data. This is |
| 6 |
sometimes called *pagination*, and uses the verb *paginate*. |
| 7 |
|
| 8 |
Call :meth:`.SQLAlchemy.paginate` on a select statement to get a :class:`.Pagination` |
| 9 |
object. |
| 10 |
|
| 11 |
During a request, this will take ``page`` and ``per_page`` arguments from the query |
| 12 |
string ``request.args``. Pass ``max_per_page`` to prevent users from requesting too many |
| 13 |
results on a single page. If not given, the default values will be page 1 with 20 items |
| 14 |
per page. |
| 15 |
|
| 16 |
.. code-block:: python |
| 17 |
|
| 18 |
page = db.paginate(db.select(User).order_by(User.join_date)) |
| 19 |
return render_template("user/list.html", page=page) |
| 20 |
|
| 21 |
|
| 22 |
Showing the Items |
| 23 |
----------------- |
| 24 |
|
| 25 |
The :class:`.Pagination` object's :attr:`.Pagination.items` attribute is the list of |
| 26 |
items for the current page. The object can also be iterated over directly. |
| 27 |
|
| 28 |
.. code-block:: jinja |
| 29 |
|
| 30 |
<ul> |
| 31 |
{% for user in page %} |
| 32 |
<li>{{ user.username }} |
| 33 |
{% endfor %} |
| 34 |
</ul> |
| 35 |
|
| 36 |
|
| 37 |
Page Selection Widget |
| 38 |
--------------------- |
| 39 |
|
| 40 |
The :class:`.Pagination` object has attributes that can be used to create a page |
| 41 |
selection widget by iterating over page numbers and checking the current page. |
| 42 |
:meth:`~.Pagination.iter_pages` will produce up to three groups of numbers, separated by |
| 43 |
``None``. It defaults to showing 2 page numbers at either edge, 2 numbers before the |
| 44 |
current, the current, and 4 numbers after the current. For example, if there are 20 |
| 45 |
pages and the current page is 7, the following values are yielded. |
| 46 |
|
| 47 |
.. code-block:: python |
| 48 |
|
| 49 |
users.iter_pages() |
| 50 |
[1, 2, None, 5, 6, 7, 8, 9, 10, 11, None, 19, 20] |
| 51 |
|
| 52 |
You can use the :attr:`~.Pagination.total` attribute to show the total number of |
| 53 |
results, and :attr:`~.Pagination.first` and :attr:`~.Pagination.last` to show the |
| 54 |
range of items on the current page. |
| 55 |
|
| 56 |
The following Jinja macro renders a simple pagination widget. |
| 57 |
|
| 58 |
.. code-block:: jinja |
| 59 |
|
| 60 |
{% macro render_pagination(pagination, endpoint) %} |
| 61 |
<div class=page-items> |
| 62 |
{{ pagination.first }} - {{ pagination.last }} of {{ pagination.total }} |
| 63 |
</div> |
| 64 |
<div class=pagination> |
| 65 |
{% for page in pagination.iter_pages() %} |
| 66 |
{% if page %} |
| 67 |
{% if page != pagination.page %} |
| 68 |
<a href="{{ url_for(endpoint, page=page) }}">{{ page }}</a> |
| 69 |
{% else %} |
| 70 |
<strong>{{ page }}</strong> |
| 71 |
{% endif %} |
| 72 |
{% else %} |
| 73 |
<span class=ellipsis>…</span> |
| 74 |
{% endif %} |
| 75 |
{% endfor %} |
| 76 |
</div> |
| 77 |
{% endmacro %} |