Django and djangorestframework views
Django views are versatile for rendering web pages, while DRF generic views streamline API development with standardized CRUD operations and serialization support.
Example
These are Python classes that provide more structure and reusability.
from rest_framework.views import APIView
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .models import Book
from .serializers import BookSerializer
class BookList(APIView):
def get(self, request):
books = Book.objects.all()
serializer = BookSerializer(books, many=True)
return Response(serializer.data)
def post(self, request):
serializer = BookSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Generic API views abstract common patterns to make view development easier and faster. DRF provides a set of built-in generic views for common tasks, such as listing, creating, retrieving, updating, and deleting objects.
from rest_framework.generics import ListAPIView
-
Used to retrieve a list of objects.
-
Used to create a new object.
-
Used to retrieve a single object.
-
Used to update an existing object.
Combining Multiple Generic API Views
For more complex workflows, you can combine multiple generic views to handle different actions.
Using ViewSets
Viewsets combine the logic for multiple views into a single class. They are a more concise way to handle standard create, retrieve, update, and delete actions.
from rest_framework import viewsets
from .models import Book
from .serializers import BookSerializer
class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import BookViewSet
router = DefaultRouter()
router.register(r'books', BookViewSet)
urlpatterns = [
path('', include(router.urls)),
]
Django Generic Views and Generic API Views
Django Generic Views
View Name | Description | Example Usage | Import |
---|---|---|---|
ListView | Displays a list of objects. | class BookListView(ListView) |
from django.views.generic import ListView |
DetailView | Displays a single object. | class BookDetailView(DetailView) |
from django.views.generic import DetailView |
CreateView | Displays a form for creating a new object and handles form submission. | class BookCreateView(CreateView) |
from django.views.generic import CreateView |
UpdateView | Displays a form for updating an existing object and handles form submission. | class BookUpdateView(UpdateView) |
from django.views.generic import UpdateView |
DeleteView | Displays a confirmation page and deletes an existing object. | class BookDeleteView(DeleteView) |
from django.views.generic import DeleteView |
TemplateView | Renders a template. | class MyTemplateView(TemplateView) |
from django.views.generic import TemplateView |
RedirectView | Redirects to a given URL. | class MyRedirectView(RedirectView) |
from django.views.generic import RedirectView |
FormView | Displays a form and handles form submission. | class MyFormView(FormView) |
from django.views.generic import FormView |
ArchiveIndexView | Displays an archive index of date-based items. | class MyArchiveIndexView(ArchiveIndexView) |
from django.views.generic.dates import ArchiveIndexView |
YearArchiveView | Displays an archive of items for a specific year. | class MyYearArchiveView(YearArchiveView) |
from django.views.generic.dates import YearArchiveView |
MonthArchiveView | Displays an archive of items for a specific month. | class MyMonthArchiveView(MonthArchiveView) |
from django.views.generic.dates import MonthArchiveView |
WeekArchiveView | Displays an archive of items for a specific week. | class MyWeekArchiveView(WeekArchiveView) |
from django.views.generic.dates import WeekArchiveView |
DayArchiveView | Displays an archive of items for a specific day. | class MyDayArchiveView(DayArchiveView) |
from django.views.generic.dates import DayArchiveView |
TodayArchiveView | Displays an archive of items for today. | class MyTodayArchiveView(TodayArchiveView) |
from django.views.generic.dates import TodayArchiveView |
DateDetailView | Displays a single object for a specific date. | class MyDateDetailView(DateDetailView) |
from django.views.generic.dates import DateDetailView |
DRF Generic API Views
View Name | Description | Example Usage | Import |
---|---|---|---|
ListAPIView | Used for read-only endpoints to represent a collection of model instances. | class BookListView(ListAPIView) |
from rest_framework.generics import ListAPIView |
CreateAPIView | Used for create-only endpoints. | class BookCreateView(CreateAPIView) |
from rest_framework.generics import CreateAPIView |
RetrieveAPIView | Used for read-only endpoints to represent a single model instance. | class BookDetailView(RetrieveAPIView) |
from rest_framework.generics import RetrieveAPIView |
UpdateAPIView | Used for update-only endpoints for a single model instance. | class BookUpdateView(UpdateAPIView) |
from rest_framework.generics import UpdateAPIView |
DestroyAPIView | Used for delete-only endpoints for a single model instance. | class BookDeleteView(DestroyAPIView) |
from rest_framework.generics import DestroyAPIView |
ListCreateAPIView | Used for read-write endpoints to represent a collection of model instances. | class BookListCreateView(ListCreateAPIView) |
from rest_framework.generics import ListCreateAPIView |
RetrieveUpdateAPIView | Used for read or update endpoints to represent a single model instance. | class BookRetrieveUpdateView(RetrieveUpdateAPIView) |
from rest_framework.generics import RetrieveUpdateAPIView |
RetrieveDestroyAPIView | Used for read or delete endpoints to represent a single model instance. | class BookRetrieveDestroyView(RetrieveDestroyAPIView) |
from rest_framework.generics import RetrieveDestroyAPIView |
RetrieveUpdateDestroyAPIView | Used for read, update or delete endpoints to represent a single model instance. | class BookRetrieveUpdateDestroyView(RetrieveUpdateDestroyAPIView) |
from rest_framework.generics import RetrieveUpdateDestroyAPIView |