summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorRussell Keith-Magee <russell@keith-magee.com>2010-11-20 05:10:13 +0000
committerRussell Keith-Magee <russell@keith-magee.com>2010-11-20 05:10:13 +0000
commit591ad8afbf7a2b32825e81866f387d4eb1938a4e (patch)
treef5142fd0a45b93a343ad99836d68479dfea74833 /docs
parentec7c49f2534284b1d9e67141d660a174ffadb3eb (diff)
Fixed #14512 -- Added documentation on how to apply decorators to class-based generic views. Thanks to Łukasz Rekucki for his work on the issue.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14642 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'docs')
-rw-r--r--docs/topics/class-based-views.txt57
1 files changed, 57 insertions, 0 deletions
diff --git a/docs/topics/class-based-views.txt b/docs/topics/class-based-views.txt
index f0e4910c51..5b848e8115 100644
--- a/docs/topics/class-based-views.txt
+++ b/docs/topics/class-based-views.txt
@@ -537,3 +537,60 @@ Because of the way that Python resolves method overloading, the local
:func:`render_to_response()` implementation will override the
versions provided by :class:`JSONResponseMixin` and
:class:`~django.views.generic.detail.SingleObjectTemplateResponseMixin`.
+
+Decorating class-based views
+============================
+
+.. highlightlang:: python
+
+The extension of class-based views isn't limited to using mixins. You
+can use also use decorators.
+
+Decorating in URLconf
+---------------------
+
+The simplest way of decorating class-based views is to decorate the
+result of the :meth:`~django.views.generic.base.View.as_view` method.
+The easiest place to do this is in the URLconf where you deploy your
+view::
+
+ from django.contrib.auth.decorators import login_required
+ from django.views.generic import TemplateView
+
+ urlpatterns = patterns('',
+ (r'^about/',login_required(TemplateView.as_view(template_name="secret.html"))),
+ )
+
+This approach applies the decorator on a per-instance basis. If you
+want every instance of a view to be decorated, you need to take a
+different approach.
+
+Decorating the class
+--------------------
+
+To decorate every instance of a class-based view, you need to decorate
+the class definition itself. To do this you apply the decorator to one
+of the view-like methods on the class; that is,
+:meth:`~django.views.generic.base.View.dispatch`, or one of the HTTP
+methods (:meth:`~django.views.generic.base.View.get`,
+:meth:`~django.views.generic.base.View.post` etc).
+
+A method on a class isn't quite the same as a standalone function, so
+you can't just apply a function decorator to the method -- you need to
+transform it into a method decorator first. The ``method_decorator``
+decorator transforms a function decorator into a method decorator so
+that it can be used on an instance method.
+
+ from django.contrib.auth.decorators import login_required
+ from django.utils.decorators import method_decorator
+ from django.views.generic import TemplateView
+
+ class ProtectedView(TemplateView):
+ template_name = 'secret.html'
+
+ @method_decorator(login_required)
+ def dispatch(self, **kwargs):
+ return super(ProtectedView, self).dispatch(**kwargs)
+
+In this example, every instance of :class:`ProtectedView` will have
+login protection.