Hi all, As a web developer primarily using django, i've been pleasantly surprised by the obvious advantages of using class based views, whenever they are applicable. So far they don't always seem to offer the most elegant solution for a specific problem, but obviously i simply use view functions in those cases.
My main issue with class based views at this point (in which im obviously not alone), is the way they interact with view decorators. The best generally accepted way of doing this as far as i've seen involves implementing the dispatch(), which usually only calls its super(), and using the method_decorator()ed version of the view decorator on it. While this solution isn't particularly complicated, it seems overly un-pythonic, and a major step back in readability and elegance from the way they used to work, and still work for function based views. Recently i've written a very simple patch function, that simply takes a view decorator, and returns a new version of the decorator, capable to be used both on a view function, or a view class. While i would by no means suggest that this should be the exact way this issue should be addressed, i think it clearly shows that making decorators work "the way they should" (as in: similar in readability to what we're used to) is actually not that difficult. Personally i plan to further develop and use this patch in the projects i'm working on, but i would be more than interested to hear what everyone else's thoughts on this are. It may be worth mentioning that class decorators are not support in python 2.5 and older, in which case the patched decorator could still be used on a view function anyway the following code should demonstrate what i'm talking about here: from django.utils.decorators import method_decorator from inspect import isfunction class _cbv_decorate(object): def __init__(self, dec): self.dec = method_decorator(dec) def __call__(self, obj): obj.dispatch = self.dec(obj.dispatch) return obj def patch_view_decorator(dec): ''' patch a view decorator, so it works properly as a decorator on both class- and function based views ''' def _conditional(view): if isfunction(view): return dec(view) return _cbv_decorate(dec)(view) return _conditional # and this is how we patch a decorator from django.contrib.auth.decorators import login_required login_required = patch_view_decorator(login_required) # this is how we can write a traditional style decorator, while still making it compatible with a cbv @patch_view_decorator def foo(func): pass -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.