From Django class-based views to service functions

From Django class-based views to service functions 图片 1

If you use Django class-based views (CBV), do you feel like it takes significant mental effort to wrap your head around the logic spread across various mixins? I have never been a big fan of Django’s class-based views (CBV) and their mixin approach. In my experience, classical function-based views (FBV) work as well as CBV, but provide more readable code.

What are the problems of class-based views

The logic stops being linear. Instead of running the code sequentially as written, the execution flow jumps between different small functions and classes in seemingly random order. Some classes belong to your code, while others are provided by the framework. Some methods extend methods of superclasses and call super(), forcing you to go down deeper into the rabbit hole. Serving a noble purpose of code de-duplication, class-based views often offer the cure that is worse than the disease. The code gets messy in an attempt to save or reuse a few lines of code.

添加评论
点赞收藏
点踩分享查看原文
评论
?
参与讨论