Django REST Framework (DRF) is the standard tool for building APIs in Python. Whether you're building a startup MVP or a full-stack app with a React frontend, DRF makes it fast, clean, and production-ready. This tutorial walks you through building your first REST API from scratch — no prior DRF experience needed.
What is Django REST Framework?
Django REST Framework is a powerful toolkit built on top of Django that makes it easy to build Web APIs. It handles serialization (converting Python objects to JSON), authentication, permissions, and routing — all with minimal boilerplate.
Why DRF over plain Django views?
- Serializers — automatic model-to-JSON conversion with validation
- ViewSets — full CRUD in ~5 lines of code
- Browsable API — a built-in UI to test your endpoints in the browser
- Authentication — JWT, session, token auth all built in
It's used by companies like Mozilla, Red Hat, and Heroku. For Indian startups and freelance projects, it's the fastest way to ship a backend.
Setting Up Your Django Project
Start with a clean virtual environment:
# Create and activate virtual environment
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
# Install Django and DRF
pip install django djangorestframework
# Create project and app
django-admin startproject myapi .
python manage.py startapp products
Add to INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
...
'rest_framework',
'products',
]
Create your model in products/models.py:
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=200)
price = models.DecimalField(max_digits=10, decimal_places=2)
description = models.TextField(blank=True)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name
python manage.py makemigrations
python manage.py migrate
Creating Your First API Endpoint
Create products/serializers.py:
from rest_framework import serializers
from .models import Product
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = '__all__'
In products/views.py, use a ModelViewSet for instant CRUD:
from rest_framework import viewsets
from .models import Product
from .serializers import ProductSerializer
class ProductViewSet(viewsets.ModelViewSet):
queryset = Product.objects.all()
serializer_class = ProductSerializer
Wire up URLs in myapi/urls.py:
from django.contrib import admin
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from products.views import ProductViewSet
router = DefaultRouter()
router.register(r'products', ProductViewSet)
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include(router.urls)),
]
Run the server: python manage.py runserver
Visit http://127.0.0.1:8000/api/products/ — you'll see DRF's browsable API. Your endpoints:
GET /api/products/— list allPOST /api/products/— createGET /api/products/1/— retrievePUT /api/products/1/— updateDELETE /api/products/1/— delete
Testing with Thunder Client
Thunder Client is a VS Code extension — a lightweight Postman alternative. Install it from the VS Code marketplace, then:
- Open Thunder Client → New Request
- Set method to
POST, URL tohttp://127.0.0.1:8000/api/products/ - Go to Body → JSON, paste:
{"name": "Test Product", "price": "499.00"} - Hit Send — you should get a
201 Createdresponse - Switch to
GETand hit Send again — your product appears in the list
Thunder Client saves your requests as a collection, so you can re-run them anytime. Much faster than writing curl commands.
Deploying to Render for Free
Render gives you a free tier for Django apps. Here's the setup:
- Add
gunicornandwhitenoiseto your requirements:pip install gunicorn whitenoise - Create
requirements.txt:pip freeze > requirements.txt - Add to
settings.py:ALLOWED_HOSTS = ['*']and configure WhiteNoise for static files - Push to GitHub
- Go to render.com → New Web Service → connect your repo
- Build command:
pip install -r requirements.txt - Start command:
gunicorn myapi.wsgi:application - Deploy — your API is live at a
.onrender.comURL
Free tier spins down after 15 minutes of inactivity, but it's perfect for portfolio projects and demos.
Next Steps — Connect to React Frontend
Now that your API is live, connecting it to React takes about 10 minutes:
# Install axios in your React project
npm install axios
// Fetch products in a React component
import { useEffect, useState } from 'react';
import axios from 'axios';
function Products() {
const [products, setProducts] = useState([]);
useEffect(() => {
axios.get('https://your-api.onrender.com/api/products/')
.then(res => setProducts(res.data));
}, []);
return (
<ul>
{products.map(p => <li key={p.id}>{p.name} — ₹{p.price}</li>)}
</ul>
);
}
One thing to add: CORS headers. Install django-cors-headers and add your React app's URL to CORS_ALLOWED_ORIGINS in settings. Without this, your browser will block the API calls.
From here, you can add JWT authentication, filtering, pagination, and more. This is the foundation of every React + Django full-stack project — including my GoRide app.
Need a Django Backend Built for Your Project?
I build REST APIs and full-stack Django + React applications for startups and businesses across India.
Let's Talk