Since a view is a callable that takes a web request and returns a web response, you can also define your views as class methods. Django provides base view classes for this. All of them inherit from the
View class, which handles HTTP method dispatching and other functionality. This is an alternate method to create your views.
We are going to change our
post_list view into a class-based view to use the generic
ListView offered by Django. This base view allows you to list objects of any kind.
views.py file of your
blog application and add the following code:
from django.views.generic import ListView class PostListView(ListView): queryset = Post.published.all() context_object_name = 'posts' paginate_by = 3 template_name ...