diff options
| author | Rigel Di Scala <rigel.discala@propylon.com> | 2015-07-21 21:54:37 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-08-01 08:38:03 -0400 |
| commit | 3bdaaf6777804d33ee46cdb5a889b8cc544a91f1 (patch) | |
| tree | 86399ce870c06f59584c98bc3988d9ed8c739c87 /docs | |
| parent | 1a76257b1b385ac8afd67bd36d061f508613e4d2 (diff) | |
Fixed #25146 -- Allowed method_decorator() to decorate classes.
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/ref/utils.txt | 10 | ||||
| -rw-r--r-- | docs/releases/1.9.txt | 3 | ||||
| -rw-r--r-- | docs/topics/class-based-views/intro.txt | 14 |
3 files changed, 23 insertions, 4 deletions
diff --git a/docs/ref/utils.txt b/docs/ref/utils.txt index c9deba887a..12bb351df0 100644 --- a/docs/ref/utils.txt +++ b/docs/ref/utils.txt @@ -151,11 +151,17 @@ The functions defined in this module share the following properties: .. module:: django.utils.decorators :synopsis: Functions that help with creating decorators for views. -.. function:: method_decorator(decorator) +.. function:: method_decorator(decorator, name='') - Converts a function decorator into a method decorator. See :ref:`decorating + 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<decorating-class-based-views>` for example usage. + .. versionchanged:: 1.9 + + The ability to decorate classes and the ``name`` parameter were added. + .. function:: decorator_from_middleware(middleware_class) Given a middleware class, returns a view decorator. This lets you use diff --git a/docs/releases/1.9.txt b/docs/releases/1.9.txt index 82ff5a08bc..f016a08c32 100644 --- a/docs/releases/1.9.txt +++ b/docs/releases/1.9.txt @@ -338,6 +338,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 <decorating-class-based-views>`. + Internationalization ^^^^^^^^^^^^^^^^^^^^ diff --git a/docs/topics/class-based-views/intro.txt b/docs/topics/class-based-views/intro.txt index 6724bec0da..2e5cc1bca1 100644 --- a/docs/topics/class-based-views/intro.txt +++ b/docs/topics/class-based-views/intro.txt @@ -279,8 +279,18 @@ that it can be used on an instance method. For example:: def dispatch(self, *args, **kwargs): return super(ProtectedView, self).dispatch(*args, **kwargs) -In this example, every instance of ``ProtectedView`` will have -login protection. +Or, more succinctly, you can decorate the class instead and pass the name +of the method to be decorated as the keyword argument ``name``:: + + @method_decorator(login_required, name='dispatch') + class ProtectedView(TemplateView): + template_name = 'secret.html' + +.. versionchanged:: 1.9 + + The ability to use ``method_decorator()`` on a class was added. + +In this example, every instance of ``ProtectedView`` will have login protection. .. note:: |
