🌐 Day 9: Django REST Framework (DRF) – Beginner’s Web Dev Series

Welcome to Day 9 of the Django for Beginners series!
Today, we step into the world of APIs — specifically, building RESTful APIs using Django REST Framework (DRF).

APIs allow your Django backend to communicate with frontend frameworks, mobile apps, and external services. Let’s begin by setting up DRF and exploring how it works.


📍 What You’ll Learn

  1. What is Django REST Framework?

  2. How to install and configure DRF

  3. How to create serializers for Django models

  4. How to build basic API views (GET & POST)

  5. How to test API endpoints using tools like Postman or DRF’s browser


📦 1. What is Django REST Framework?

Django REST Framework (DRF) is a powerful toolkit for building Web APIs using Django. It provides easy tools for serializing data, handling requests, authenticating users, and structuring API views.


🛠️ 2. Install & Setup DRF

Install the package:

pip install djangorestframework

Add to INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'rest_framework',
]

🧱 3. Create a Model to Work With

Example:

# models.py

class Student(models.Model):
    name = models.CharField(max_length=100)
    age = models.IntegerField()
    email = models.EmailField()

🔄 4. Create a Serializer

# serializers.py

from rest_framework import serializers
from .models import Student

class StudentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Student
        fields = '__all__'

🔍 5. Create a Basic API View

# views.py

from rest_framework.response import Response
from rest_framework.decorators import api_view
from .models import Student
from .serializers import StudentSerializer

@api_view(['GET', 'POST'])
def student_api(request):
    if request.method == 'GET':
        students = Student.objects.all()
        serializer = StudentSerializer(students, many=True)
        return Response(serializer.data)

    if request.method == 'POST':
        serializer = StudentSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=201)
        return Response(serializer.errors, status=400)

🌐 6. Add a URL Route

# urls.py

from django.urls import path
from .views import student_api

urlpatterns = [
    path('api/students/', student_api, name='student_api'),
]

📫 7. Sending a POST Request (Payload)

In Postman or any HTTP client, send:

URL: http://127.0.0.1:8000/api/students/
Method
: POST
Headers: Content-Type: application/json
Body (JSON):

{
  "name": "Shankar",
  "age": 25,
  "email": "shankar@example.com"
}

✅ You'll receive the saved object back as JSON.


🔐 8. DRF Test Interface

When you visit /api/students/ in the browser, DRF shows an interactive API interface — where you can test GET and POST easily.


✅ Task for Day 9

  • Install and configure Django REST Framework

  • Create a model and serializer

  • Build an API view that supports GET and POST

  • Test it using browser or Postman

  • Save at least one new student via POST


🚀 Coming Up: Day 10 – Django Deployment

Next, we’ll learn:

  • How to deploy Django on render

0
Subscribe to my newsletter

Read articles from Shankar Lamichhane directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shankar Lamichhane
Shankar Lamichhane

Hi, I’m Shankar — a Sr. Software Engineer specializing in Python, Django, and DevOps. I build scalable web applications, APIs, and cloud-native systems, with a focus on clean architecture and backend automation.