Next

DRF Introduction

  • Introduction to Django REST Framework and building RESTful APIs in Django.
  • What is DRF?

    Django REST Framework (DRF) is a powerful toolkit used to build Web APIs using Django.

    In simple words:

    DRF helps Django applications send and receive data in JSON format so that mobile apps, frontend apps, and other systems can communicate with Django.

    Why Do We Need DRF?

    Traditional Django:

    • Returns HTML pages

    • Works best for web browsers

    Modern applications need:

    • Mobile apps

    • React / Angular / Vue frontend

    • Third-party integrations

    DRF solves this problem by creating APIs

    Real-World Example

    Platform

    Uses API

    Mobile App

    Fetch data from server

    React App

    Uses backend API

    Payment Gateway

    Communicates via API

    Admin Panel

    API based

    What is an API?

    API (Application Programming Interface)

    An API is a communication bridge between two systems.

    Example:

    Client (Browser / App)

          ↓ API Request

    Server (Django)

          ↓ API Response (JSON)

    API Response Format

    Most APIs use JSON:

    {

      "id": 1,

      "name": "Django Course",

      "price": 5000

    }

    API vs Website

    Website

    API

    Returns HTML

    Returns JSON

    For users

    For applications

    Template based

    Data based

    REST Basics

    What is REST?

    REST = Representational State Transfer

    REST is a set of rules for designing APIs.

    REST Principles

    •  Client–Server separation

    • Stateless communication

    •  Uses HTTP methods

    •  Resource-based URLs

    RESTful URL Example

    /api/students/

    Represents a resource (students)

    HTTP Methods in API

    Method

    Purpose

    GET

    Fetch data

    POST

    Create data

    PUT

    Update full data

    PATCH

    Update partial data

    DELETE

    Remove data

    Example API Actions

    Action

    Method

    Get students

    GET

    Add student

    POST

    Update student

    PUT

    Delete student

    DELETE

    Why Django REST Framework?

    Problems Without DRF

    • Manual JSON handling

    • No validation system

    • Complex authentication

    • More code

    DRF Advantages

    •  Automatic serialization

    •  Browsable API

    • Authentication & permissions

    • Easy CRUD APIs

    • Fast development

    DRF Installation & Setup

Install DRF

pip install djangorestframework

Add DRF to Django Project

settings.py

INSTALLED_APPS = [
    'rest_framework',
]
  •  Description

    • Enables DRF features

    • Required for API development

    First Simple API Without DRF  

JSON Response Using Django

from django.http import JsonResponse

def student_api(request):
    data = {
        "name": "Rahul",
        "course": "Django"
    }
    return JsonResponse(data)
  • Works, but:

    • No validation

    • No structure

    • No scalability

    First API Using DRF

    DRF APIView Example

Simple DRF APIView

views.py

from rest_framework.views import APIView
from rest_framework.response import Response

class HelloAPI(APIView):
    def get(self, request):
        return Response({
            "message": "Hello from DRF"
        })

API URL Configuration

urls.py

from django.urls import path
from .views import HelloAPI

urlpatterns = [
    path('api/hello/', HelloAPI.as_view()),
]
  • Description

    • APIView → DRF base class

    • Response → returns JSON automatically

    • Clean & structured

    Browsable API (DRF Feature)

    What is Browsable API?

    DRF provides a web interface to test APIs.

    Visit in browser:

    http://127.0.0.1:8000/api/hello/

    You can:

    • Send GET/POST

    • See JSON output

    • Debug easily

    DRF Request–Response Cycle

    Client Request

          ↓

    DRF View

          ↓

    Serializer

          ↓

    Response (JSON)

    DRF handles:

    • Parsing data

    • Validation

    • Formatting response

Next