4đź‘Ť
Certainly there’s nothing wrong with using a class for a view, provided you route the URL to an actual instance of a class and not just a class directly.
2đź‘Ť
The Django admin does exactly this – look at the source code in django/contrib/admin.
The advantage of classes is that they are much easier to customize, for example you can add hooks for permission checking.
There is a proposal to move all existing generic views over to classes, it was supposed to get into 1.2 but failed to meet the deadline.
As the above poster points out, be very careful about handling instance variables – if you look at the admin classes, you see the request being passed to the various methods instead of relying on “self”.
0đź‘Ť
Setting aside other concerns (such as thread-safety issues), it feels like there’s a real possible danger here to cross the bright lines between Model / View / Template.
Or maybe it feels like a replacement for url dispatching (not that there’s anything wrong with that :-). I’m not sure, but it just feels slightly off.
- [Django]-Using CKEditor on Django Blog, How can I get django to render the html from ckeditor?
- [Django]-Django Custom User Model Best Practice: User = get_user_model()?
0đź‘Ť
While class-based views are useful, inheritance may not be the right tool for this particular job. Helper functions and decorators are two great ways to factor out common code from your views. They also tend to be be more familiar/natural to other (python) coders who might work on your code.
I’m not sure what the best approach is in your case as I don’t know how much you ultimately want to factor, just keep in mind that there are other ways to factor in python besides inheritance.
p.s. kudos for seeking out a pythonic solution.
- [Django]-NoReverseMatch error when creating new users through admin
- [Django]-Django – Using context_processor
- [Django]-Django Rest Framework – GenericViewSet with Authentication/Permission decorator