The goal of this tutorial is to provide a comprehensive understanding of API Views and ViewSets in Django REST Framework. By the end of this tutorial, you will learn how to define HTTP methods using API Views and ViewSets, and how they simplify the logic behind routing and handling requests.
Prerequisites:
- Basic understanding of Python
- Familiarity with Django and Django REST Framework
API Views and ViewSets are two important concepts in Django REST Framework. API Views determine the logic of how requests are handled. ViewSets, on the other hand, are just a type of class-based View, that do not provide any method handlers but are used for defining the view behavior.
API Views: An API View, is where you define the logic of your API. It determines how requests are handled. API Views are Python classes and can include methods to handle HTTP verbs like get, post, put, patch, delete.
ViewSets: A ViewSet is a simple, reusable class-based View, that does not provide any method handlers such as .get() or .post(), but provides actions such as .list(), .retrieve(), .create(), .update(), .partial_update(), and .destroy().
Example: API View
from rest_framework.views import APIView
from rest_framework.response import Response
class HelloWorld(APIView):
def get(self, request):
return Response({"message": "Hello, World!"})
In this code snippet, we've created a simple APIView that handles GET requests. When a GET request is made, it returns a HTTP response with the message "Hello, World!".
Example: ViewSet
from rest_framework import viewsets
from rest_framework.response import Response
class HelloWorld(viewsets.ViewSet):
def list(self, request):
return Response({"message": "Hello, World!"})
In this example, we've created a simple ViewSet that handles the list action. When a GET request is made, it returns a HTTP response with the message "Hello, World!".
In this tutorial, we have learned about API Views and ViewSets in Django REST Framework. We have also seen how to use them to handle HTTP requests. Practice what you've learned with the exercises below.
For further learning, you can explore how to handle different types of HTTP requests with API Views and ViewSets, how to add authentication and permissions to your views, and how to use routers with ViewSets.
Exercise 1: Create an APIView that handles POST requests and returns the data sent in the request.
Exercise 2: Create a ViewSet that handles the create action and returns the data sent in the request.
Solutions:
Solution 1:
class Echo(APIView):
def post(self, request):
return Response({"received_data": request.data})
This APIView handles POST requests and simply echoes back the data sent in the request.
Solution 2:
class Echo(viewsets.ViewSet):
def create(self, request):
return Response({"received_data": request.data})
This ViewSet handles the create action and echoes back the data sent in the request.
Further Practice: Try handling other types of HTTP requests and actions. Try adding authentication and permissions to your views. Try using routers with ViewSets.