DEV Community

Muhammad Atif Iqbal
Muhammad Atif Iqbal

Posted on

What is difference between Django and DRF ( Django REST Framework )

๐Ÿ”น 1. Django

  • What it is:
    Django is a full-stack web framework for Python.
    It helps you build websites and web apps (with HTML templates, forms, authentication, ORM, admin panel, etc.).

  • Main focus:
    Django is designed to create server-rendered web pages (like traditional websites).

  • Example use case:

    • Blog website
    • E-commerce site
    • Admin dashboard with HTML pages
  • Typical flow:

    Browser โ†’ URL โ†’ Django view โ†’ HTML template โ†’ Browser displays webpage.


๐Ÿ”น 2. Django REST Framework (DRF)

  • What it is:
    DRF is a third-party library built on top of Django.
    Itโ€™s not a separate framework โ€” it extends Django to build REST APIs.

  • Main focus:
    DRF is designed to send and receive data (usually JSON), not HTML.
    It turns your Django models into RESTful API endpoints.

  • Example use case:

    • Mobile app backend (iOS/Android need JSON, not HTML)
    • SPA frontend (React, Vue, Angular) that fetches data via API
    • Exposing APIs to third-party developers
  • Typical flow:

    Mobile App / React Frontend โ†’ API Request โ†’ DRF view โ†’ JSON Response.


๐Ÿ”น 3. Key Differences

Feature Django DRF (Django REST Framework)
Purpose Build websites (HTML templates) Build APIs (JSON, XML, etc.)
Response Type HTML (via templates) JSON / other API formats
Views views.py with HttpResponse/render() APIView, ViewSet, GenericViewSet
Serialization Not needed (works with templates) Required (Serializer converts model โ†’ JSON and back)
Authentication Session-based auth (cookies) Token/JWT/OAuth (API-friendly)
Front-end Integration Tightly coupled (Django renders HTML) Decoupled (React, Vue, Flutter, Mobile apps use APIs)

๐Ÿ”น 4. Example

Django View (HTML)

# views.py
from django.shortcuts import render
from .models import Article

def article_list(request):
    articles = Article.objects.all()
    return render(request, "articles.html", {"articles": articles})
Enter fullscreen mode Exit fullscreen mode

This returns an HTML page.


DRF API View (JSON)

# views.py
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import Article
from .serializers import ArticleSerializer

class ArticleListAPI(APIView):
    def get(self, request):
        articles = Article.objects.all()
        serializer = ArticleSerializer(articles, many=True)
        return Response(serializer.data)
Enter fullscreen mode Exit fullscreen mode

This returns JSON like:

[
  {"id": 1, "title": "Hello World", "content": "First article"},
  {"id": 2, "title": "Another Post", "content": "Second article"}
]
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น 5. When to use which?

  • Django only โ†’ If youโ€™re building a traditional website where backend renders HTML (like WordPress-style blog, admin dashboards).
  • Django + DRF โ†’ If youโ€™re building a backend for mobile apps, SPAs (React, Vue), or APIs for third-party use.

โœ… In short:

  • Django โ†’ Web pages
  • DRF โ†’ APIs (JSON)

Top comments (0)