From dfc720c521062cdefd64bc48a11838b0fa583439 Mon Sep 17 00:00:00 2001 From: Marcelo Galigniana Date: Sun, 9 Apr 2023 22:30:40 -0300 Subject: Fixed #27505 -- Allowed customizing Paginator's error messages. --- docs/ref/paginator.txt | 36 +++++++++++++++++++++++++++++++++++- docs/releases/5.0.txt | 6 ++++++ 2 files changed, 41 insertions(+), 1 deletion(-) (limited to 'docs') 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 `. ``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 `. ``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 ` 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 ~~~~~~~~~~~~~~~~~~~~~~ -- cgit v1.2.1