Junior — Middle
Can you list and describe the main view classes used in Django?
sobes.tech AI
Answer from AI
In Django, the main classes of views are divided into two categories: functional and class-based.
-
Function-based views (FBV) — regular functions that accept a request and return a response.
-
Class-based views (CBV) — classes that encapsulate request processing logic and allow code reuse.
Main classes of class-based views:
View— the base class for all class-based views.TemplateView— displays a template without additional logic.ListView— displays a list of model objects.DetailView— displays details of a single object.CreateView— a form for creating a new object.UpdateView— a form for editing an existing object.DeleteView— confirmation and deletion of an object.
These classes simplify CRUD operations and allow for quick creation of standard pages.
Example of using ListView:
from django.views.generic import ListView
from .models import Article
class ArticleListView(ListView):
model = Article
template_name = 'articles/list.html'
context_object_name = 'articles'