Handling GET, POST, PUT, and DELETE Requests Manually in Django REST Framework
Last updated 1 month, 2 weeks ago | 170 views 75 5

When building RESTful APIs, understanding how to manually handle HTTP methods like GET
, POST
, PUT
, and DELETE
is essential. While Django REST Framework (DRF) provides powerful abstractions like ModelViewSet
, sometimes you need more control — and that’s where manual method handling shines.
In this guide, you'll learn:
-
What these HTTP methods do
-
How to handle each method manually using
APIView
-
A full working example
-
Common tips and pitfalls
What Do These HTTP Methods Do?
Method | Purpose |
---|---|
GET | Retrieve data (e.g., get a list or single item) |
POST | Create a new resource |
PUT | Update an entire resource |
DELETE | Remove a resource |
Setup: Sample Book
Model
Let’s use this simple model:
# models.py
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)
Serializer
# serializers.py
from rest_framework import serializers
from .models import Book
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = ['id', 'title', 'author']
Handling HTTP Methods Manually with APIView
We'll create two views:
-
One to handle listing and creating books (
GET
andPOST
) -
One to handle retrieving, updating, and deleting a single book (
GET
,PUT
,DELETE
)
Book List & Create View
# views.py
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 BookListCreateAPIView(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)
Book Detail, Update, and Delete View
class BookDetailAPIView(APIView):
def get_object(self, pk):
try:
return Book.objects.get(pk=pk)
except Book.DoesNotExist:
return None
def get(self, request, pk):
book = self.get_object(pk)
if not book:
return Response({"error": "Book not found"}, status=404)
serializer = BookSerializer(book)
return Response(serializer.data)
def put(self, request, pk):
book = self.get_object(pk)
if not book:
return Response({"error": "Book not found"}, status=404)
serializer = BookSerializer(book, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=400)
def delete(self, request, pk):
book = self.get_object(pk)
if not book:
return Response({"error": "Book not found"}, status=404)
book.delete()
return Response(status=204)
URLs Configuration
# urls.py
from django.urls import path
from .views import BookListCreateAPIView, BookDetailAPIView
urlpatterns = [
path('books/', BookListCreateAPIView.as_view()),
path('books/<int:pk>/', BookDetailAPIView.as_view()),
]
Tips for Manual Method Handling
-
✅ Always validate request data using serializers.
-
✅ Use
status
from DRF for readable HTTP status codes. -
✅ Implement proper error handling for missing objects.
-
✅ Use
Response()
from DRF instead of Django'sHttpResponse
.
⚠️ Common Pitfalls
Pitfall | How to Avoid |
---|---|
Forgetting .as_view() |
Required for class-based views in urls.py |
Not handling 404 errors | Use helper like get_object_or_404() or manual checks |
Returning incorrect status codes | Use DRF’s status module (status.HTTP_201_CREATED , etc.) |
Not validating input data | Always check serializer.is_valid() before saving |
Complete Sample JSON API Flow
POST /books/
{
"title": "Atomic Habits",
"author": "James Clear"
}
Response:
{
"id": 1,
"title": "Atomic Habits",
"author": "James Clear"
}
GET /books/
Response:
[
{
"id": 1,
"title": "Atomic Habits",
"author": "James Clear"
}
]
PUT /books/1/
{
"title": "Atomic Habits - Updated",
"author": "James Clear"
}
DELETE /books/1/
Response: 204 No Content
Final Thoughts
Handling GET
, POST
, PUT
, and DELETE
requests manually with APIView
gives you complete control over how data is fetched, created, updated, and deleted. It’s perfect for customized endpoints, special logic, or when DRF’s automatic tools are too opinionated.