A function-based view is a plain function that turns a request into a response, while a class-based view inherits from one of Django's view classes. For the URL configuration, as_view() turns the class into a callable.
def article_list(request):
articles = Article.objects.all()
return render(request, 'articles/list.html', {'articles': articles})
Function-based views are simpler and more explicit. You read the whole flow top to bottom, and you branch on request.method yourself.
from django.views.generic import ListView
class ArticleListView(ListView):
model = Article
template_name = 'articles/list.html'
context_object_name = 'articles'
Class-based views provide more structure and reusability. Each HTTP method gets its own method instead of an if branch. Mixins, small classes whose methods several views inherit, share behaviour across views. The generic ListView above queries the articles and renders the template with attributes only, no methods.
The cost is that the flow is spread across parent classes you have to read.
Use a function for one-off or unusual logic. Use a class when the view follows a common pattern or shares code with others.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
The diagram below the answer is the concept . Jump to it ↓