Performance: Why class-based views exist
MEDIUM IMPACT
Class-based views affect server response time and code maintainability, indirectly impacting page load speed by enabling efficient view reuse and reducing server processing overhead.
from django.views import View class MyView(View): def get(self, request): # handle GET pass def post(self, request): # handle POST pass
def my_view(request): if request.method == 'GET': # handle GET pass elif request.method == 'POST': # handle POST pass # repeated code for other methods
| Pattern | Server CPU Time | Code Complexity | Response Time | Verdict |
|---|---|---|---|---|
| Function-based views with many conditionals | High due to branching | High, hard to maintain | Slower response | [X] Bad |
| Class-based views with method handlers | Lower due to modular code | Low, easier to maintain | Faster response | [OK] Good |