summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorMarcelo Galigniana <marcelogaligniana@gmail.com>2023-04-09 22:30:40 -0300
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-04-12 14:02:28 +0200
commitdfc720c521062cdefd64bc48a11838b0fa583439 (patch)
tree8b38ad33d223e8c56f21a609fe504ac06cef5b92 /docs
parent041b0a359a0a80e147b47c6ae5f11bca9dd3b28a (diff)
downloaddjango-dfc720c521062cdefd64bc48a11838b0fa583439.tar.gz
Fixed #27505 -- Allowed customizing Paginator's error messages.
Diffstat (limited to 'docs')
-rw-r--r--docs/ref/paginator.txt36
-rw-r--r--docs/releases/5.0.txt6
2 files changed, 41 insertions, 1 deletions
diff --git a/docs/ref/paginator.txt b/docs/ref/paginator.txt
index 670c06e6a8..8afadbe6f8 100644
--- a/docs/ref/paginator.txt
+++ b/docs/ref/paginator.txt
@@ -14,7 +14,7 @@ For examples, see the :doc:`Pagination topic guide </topics/pagination>`.
``Paginator`` class
===================
-.. class:: Paginator(object_list, per_page, orphans=0, allow_empty_first_page=True)
+.. class:: Paginator(object_list, per_page, orphans=0, allow_empty_first_page=True, error_messages=None)
A paginator acts like a sequence of :class:`Page` when using ``len()`` or
iterating it directly.
@@ -56,6 +56,40 @@ For examples, see the :doc:`Pagination topic guide </topics/pagination>`.
``False`` and ``object_list`` is empty, then an ``EmptyPage`` error will
be raised.
+.. attribute:: Paginator.error_messages
+
+ .. versionadded:: 5.0
+
+ The ``error_messages`` argument lets you override the default messages that
+ the paginator will raise. Pass in a dictionary with keys matching the error
+ messages you want to override. Available error message keys are:
+ ``invalid_page``, ``min_page``, and ``no_results``.
+
+ For example, here is the default error message:
+
+ .. code-block:: pycon
+
+ >>> from django.core.paginator import Paginator
+ >>> paginator = Paginator([1, 2, 3], 2)
+ >>> paginator.page(5)
+ Traceback (most recent call last):
+ ...
+ EmptyPage: That page contains no results
+
+ And here is a custom error message:
+
+ .. code-block:: pycon
+
+ >>> paginator = Paginator(
+ ... [1, 2, 3],
+ ... 2,
+ ... error_messages={"no_results": "Page does not exist"},
+ ... )
+ >>> paginator.page(5)
+ Traceback (most recent call last):
+ ...
+ EmptyPage: Page does not exist
+
Methods
-------
diff --git a/docs/releases/5.0.txt b/docs/releases/5.0.txt
index cc4fb69ee3..0d1e7ffda1 100644
--- a/docs/releases/5.0.txt
+++ b/docs/releases/5.0.txt
@@ -284,6 +284,12 @@ Models
:ref:`Choices classes <field-choices-enum-types>` directly instead of
requiring expansion with the ``choices`` attribute.
+Pagination
+~~~~~~~~~~
+
+* The new :attr:`django.core.paginator.Paginator.error_messages` argument
+ allows customizing the error messages raised by :meth:`.Paginator.page`.
+
Requests and Responses
~~~~~~~~~~~~~~~~~~~~~~