From 186eb21dc159807dba83148f7c9c50d470745708 Mon Sep 17 00:00:00 2001 From: fabrizio ettore messina Date: Tue, 11 Aug 2015 13:35:50 +0200 Subject: Fixed #25269 -- Allowed method_decorator() to accept a list/tuple of decorators. --- docs/ref/utils.txt | 14 +++++++++++--- docs/releases/1.9.txt | 5 +++-- docs/topics/class-based-views/intro.txt | 22 +++++++++++++++++++++- 3 files changed, 35 insertions(+), 6 deletions(-) (limited to 'docs') diff --git a/docs/ref/utils.txt b/docs/ref/utils.txt index c1df458872..c24551b9ec 100644 --- a/docs/ref/utils.txt +++ b/docs/ref/utils.txt @@ -155,12 +155,20 @@ The functions defined in this module share the following properties: Converts a function decorator into a method decorator. It can be used to decorate methods or classes; in the latter case, ``name`` is the name - of the method to be decorated and is required. See :ref:`decorating - class-based views` for example usage. + of the method to be decorated and is required. + + ``decorator`` may also be a a list or tuple of functions. They are wrapped + in reverse order so that the call order is the order in which the functions + appear in the list/tuple. + + See :ref:`decorating class based views ` for + example usage. .. versionchanged:: 1.9 - The ability to decorate classes and the ``name`` parameter were added. + The ability to decorate classes, the ``name`` parameter, and the ability + for ``decorator`` to accept a list/tuple of decorator functions were + added. .. function:: decorator_from_middleware(middleware_class) diff --git a/docs/releases/1.9.txt b/docs/releases/1.9.txt index c2079f27d0..3f101a5f94 100644 --- a/docs/releases/1.9.txt +++ b/docs/releases/1.9.txt @@ -378,8 +378,9 @@ Generic Views * Class-based views generated using ``as_view()`` now have ``view_class`` and ``view_initkwargs`` attributes. -* :func:`~django.utils.decorators.method_decorator` can now be used to - :ref:`decorate classes instead of methods `. +* :func:`~django.utils.decorators.method_decorator` can now be used with a list + or tuple of decorators. It can also be used to :ref:`decorate classes instead + of methods `. Internationalization ^^^^^^^^^^^^^^^^^^^^ diff --git a/docs/topics/class-based-views/intro.txt b/docs/topics/class-based-views/intro.txt index 0b4a02cfbb..5e3351e90c 100644 --- a/docs/topics/class-based-views/intro.txt +++ b/docs/topics/class-based-views/intro.txt @@ -286,9 +286,29 @@ of the method to be decorated as the keyword argument ``name``:: class ProtectedView(TemplateView): template_name = 'secret.html' +If you have a set of common decorators used in several places, you can define +a list or tuple of decorators and use this instead of invoking +``method_decorator()`` multiple times. These two classes are equivalent:: + + decorators = [never_cache, login_required] + + @method_decorator(decorators, name='dispatch') + class ProtectedView(TemplateView): + template_name = 'secret.html' + + @method_decorator(never_cache, name='dispatch') + @method_decorator(login_required, name='dispatch') + class ProtectedView(TemplateView): + template_name = 'secret.html' + +The decorators will process a request in the order they are passed to the +decorator. In the example, ``never_cache()`` will process the request before +``login_required()``. + .. versionchanged:: 1.9 - The ability to use ``method_decorator()`` on a class was added. + The ability to use ``method_decorator()`` on a class and the ability for + it to accept a list or tuple of decorators were added. In this example, every instance of ``ProtectedView`` will have login protection. -- cgit v1.3