﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
15328	Class-based Views (CBV) documentation mistake on method_decorator approach for @login_required	Andy Terra	Gabriel Hurley	"http://docs.djangoproject.com/en/dev//topics/class-based-views/#decorating-the-class

The code described in the documentation does not work. My attempt to run the code is registered here: http://dpaste.com/hold/423359/

Per Łukasz Rekucki, on django-users, it should be changed to:

{{{
class ProtectedView(TemplateView):
   template_name = 'secret.html'

   @method_decorator(login_required)
   def dispatch(self, *args, **kwargs):
       return super(ProtectedView, self).dispatch(*args, **kwargs)
}}}

I also recommend mentioning alternatives to providing the same functionality, such as the LoginMixin that was suggested by on the same django-users thread:

{{{
import settings

class LoginMixin(object):
    def get_test_func(self):
        return getattr(self, 'test_func', lambda u: u.is_authenticated())
    def get_login_url(self):
        return getattr(self, 'login_url', settings.LOGIN_URL)
    def get_redirect_field_name(self):
        return getattr(self, 'redirect_field_name', None)
    def dispatch(self, request, *args, **kwargs):
        from django.contrib.auth.decorators import user_passes_test
    
        return user_passes_test(
            self.get_test_func(),
            login_url = self.get_login_url(),
            redirect_field_name = self.get_redirect_field_name()
        )(super(LoginMixin, self).dispatch
        )(request, *args, **kwargs)

}}}

Finally, thank you for providing us with this functionality and documentation. It really makes the code for my views leaner, cleaner, meaner.

[[BR]]
[[BR]]
Best regards,
[[BR]]
André Terra"		closed	Documentation	1.3-beta		fixed	CBV, class-based views, method_decorator, login_required, decorators, documentation, docs, mixin, login		Accepted	0	0	0	0	0	0
