Menu

Class-Based Views and Generic Views

Class-Based Views and Generic Views

Function-based views (FBVs) are simple and explicit. Class-based views (CBVs) trade some explicitness for reusability and less repetition — especially for common patterns like list and detail pages.

The same view, two ways

📝 For simple views, FBVs are usually cleaner. CBVs shine when you need to handle multiple HTTP methods (GET and POST in one class) or when you want to use Django's generic views.

Generic Views

Django's generic views cover the most common patterns — listing objects, showing detail pages, creating, updating, and deleting — with minimal code.

Reusable View Logic with Mixins

Mixins let you package reusable logic and combine it into any class-based view. The standard library includes LoginRequiredMixin, PermissionRequiredMixin, and UserPassesTestMixin. You can also write your own.