Class-Based Views (CBV)
Class-Based Views (CBV)
While Function-Based Views are simple and explicit, standard web applications contain repetitive CRUD operations: listing records, displaying details, creating records via forms, updating instances, and deleting rows. Django's Class-Based Views (CBVs) and Generic Class-Based Views (GCBVs) use object-oriented patterns, mixins, and inheritance to eliminate hundreds of lines of boilerplate.
1. The Class-Based View Hierarchy
2. Implementing Standard CRUD with Generic Views
3. Connecting CBVs in urls.py with as_view()
URL dispatchers expect a callable function. CBVs provide the .as_view() class method to generate the callable:
4. Why reverse_lazy is Required in Class Attributes
In class definitions, class attributes (success_url = reverse_lazy('...')) are evaluated when the Python module is first imported. Standard reverse() fails because URL patterns haven't finished loading yet. reverse_lazy() defers evaluation until the URL is actually accessed!
Practice Quiz
URL Dispatcher and Routing
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Function-Based Views (FBV) | URL Dispatcher and Routing |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.