🔹 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})
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)
This returns JSON like:
[ {"id": 1, "title": "Hello World", "content": "First article"}, {"id": 2, "title": "Another Post", "content": "Second article"} ]
🔹 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)