I can now both instantiate view classes and use the instances as view functions, OR I can simply point my URLconf to my class and have the metaclass instantiate (and call) the view class for me. Return HttpResponseBadRequest('Method Not Allowed', status=405) Instance = super(CallableViewClass, cls)._call_(*args, **kwargs)ĭef _call_(self, request, *args, **kwargs): Instance = super(CallableViewClass, cls)._call_() If args and isinstance(args, HttpRequest): What helped me was a surprisingly simple metaclass: class CallableViewClass(type):
#COUPON CODE TEXTUREPACKER CODE WEB UNITY FULL#
I needed to use class based views, but I wanted to be able to use the full name of the class in my URLconf without always having to instantiate the view class before using it. I now feel that TemplateResponse and view decorators is a better answer for decomposing view code. After having used them extensively on a couple of projects, I feel they tend to lead to code that is satisfyingly DRY to write, but very hard to read and maintain later, because functionality is spread across so many different places, and subclasses are so dependent on every implementation detail of the superclasses and mixins. UPDATE: FWIW, I've changed my opinion on class-based views since this answer was written. UPDATE: Django's own generic views are now class-based. I don't think this is much of a concern it's rare that Python code execution would be your performance bottleneck in a web app. The only real downside I see is the proliferation of internal method calls, which may impact performance somewhat. (I get tired of rewriting the same create/update view logic anytime I need to tweak something Django's generic views don't quite allow). I really like it while Django's generic views allow some customization through keyword arguments, OO generic views (if their behavior is split into a number of separate methods) can have much more fine-grained customization via subclassing, which lets me repeat myself a lot less. I've created and used my own generic view classes, defining _call_ so an instance of the class is callable.