From 591ad8afbf7a2b32825e81866f387d4eb1938a4e Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Sat, 20 Nov 2010 05:10:13 +0000 Subject: [PATCH] =?UTF-8?q?Fixed=20#14512=20--=20Added=20documentation=20o?= =?UTF-8?q?n=20how=20to=20apply=20decorators=20to=20class-based=20generic?= =?UTF-8?q?=20views.=20Thanks=20to=20=C5=81ukasz=20Rekucki=20for=20his=20w?= =?UTF-8?q?ork=20on=20the=20issue.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://code.djangoproject.com/svn/django/trunk@14642 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/utils/decorators.py | 6 +++- django/views/generic/base.py | 9 +---- docs/topics/class-based-views.txt | 57 +++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 9 deletions(-) diff --git a/django/utils/decorators.py b/django/utils/decorators.py index d75d36f753..17f2ea30b3 100644 --- a/django/utils/decorators.py +++ b/django/utils/decorators.py @@ -1,11 +1,15 @@ "Functions that help with dynamically creating decorators for views." -import types try: from functools import wraps, update_wrapper, WRAPPER_ASSIGNMENTS except ImportError: from django.utils.functional import wraps, update_wrapper, WRAPPER_ASSIGNMENTS # Python 2.4 fallback. +class classonlymethod(classmethod): + def __get__(self, instance, owner): + if instance is not None: + raise AttributeError("This method is available only on the view class.") + return super(classonlymethod, self).__get__(instance, owner) def method_decorator(decorator): """ diff --git a/django/views/generic/base.py b/django/views/generic/base.py index 19d041582c..0c9cd7971f 100644 --- a/django/views/generic/base.py +++ b/django/views/generic/base.py @@ -1,19 +1,12 @@ -import copy from django import http from django.core.exceptions import ImproperlyConfigured from django.template import RequestContext, loader -from django.utils.translation import ugettext_lazy as _ from django.utils.functional import update_wrapper from django.utils.log import getLogger +from django.utils.decorators import classonlymethod logger = getLogger('django.request') -class classonlymethod(classmethod): - def __get__(self, instance, owner): - if instance is not None: - raise AttributeError("This method is available only on the view class.") - return super(classonlymethod, self).__get__(instance, owner) - class View(object): """ Intentionally simple parent class for all views. Only implements 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.