This guide takes you step by step through building full-stack web applications using Django for the backend and React for the frontend. It explains how to use Django's powerful framework to create APIs with Django REST Framework (DRF) and how React's dynamic user interface components can consume these APIs to create interactive web applications. Starting with Django setup, the guide walks you through installing necessary tools, creating models, serializing data, and building API views. It then covers setting up React, using Axios for making API requests, and handling CORS to connect the frontend to the backend. By the end of the guide, you'll have a fully functional web application where React fetches data from Django's API and presents it to the user. This approach allows for clear separation of backend and frontend concerns, making development faster, maintainable, and scalable. Whether you're a backend Python developer or a frontend JavaScript enthusiast, this tutorial will help you leverage the strengths of both technologies for modern web development.
Have you ever dreamt of building web applications that feel as smooth as your favorite social platform, yet are backed by the solid reliability of Python? You’re not alone. The world of web development has exploded with tools and frameworks, but few combinations have sparked as much excitement and productivity as Django and React JS.
Imagine this: you’re sitting in front of your laptop, coffee steaming, eyes fixed on code that — line by line — brings your ideas to life. You want your app to be more than just functional; you want it to be beautiful, interactive, and scalable. That’s where the marriage of Django and React shines.
Django gives you a powerful, battle-tested backend built on Python’s clean syntax and philosophy of “don’t repeat yourself.” It lets you focus on your application’s logic, handling the tedious parts of web development so you don’t have to reinvent the wheel.
React JS, on the other hand, offers a radically different way to build user interfaces — components that react (pun intended!) instantly to user actions and data updates, making your app feel alive and modern.
But here’s the magic: when you connect these two technologies — using Django’s REST framework to expose your data as APIs and React to consume and present it beautifully — you gain a workflow that is both enjoyable and immensely productive. You can work faster, collaborate better, and build applications that users genuinely love.
You don’t have to be a seasoned expert to join this journey. Whether you’re a Pythonista curious about the front-end, or a JavaScript fan wanting greater backend power, this guide will walk you step by step from first setup to full-stack integration.
“The real superpower of web development today comes from combining the best tools, not confining yourself to one.”
So, let’s embark together on bringing the future of web apps into your hands — one elegant line of code at a time.
INTRODUCTION
Django is a robust Python-based web framework, lauded for rapid development and clean code.
React JS enables the creation of fast, interactive user interfaces.
The two combined allow you to separate frontend and backend concerns, making development more maintainable and scalable.
Step 1: Setting Up Django
1.1. Install Django
Explain installing Django:
pip install django1.2. Start a New Project
django-admin startproject myproject
cd myproject
python manage.py startapp apiStep 2: Introducing Django REST Framework
Explain that Django REST Framework (DRF) is the gold-standard for building APIs in Django. It offers a flexible way to serialize and expose your Django models as JSON.
2.1. Install DRF
pip install djangorestframework2.2. Add to INSTALLED_APPS
Include 'rest_framework' and your app ('api') in settings.py:
INSTALLED_APPS = [
# other apps
'rest_framework',
'api',
]2.3. Build a Simple Model
# api/models.py
from django.db import models
class Task(models.Model):
title = models.CharField(max_length=255)
completed = models.BooleanField(default=False)
def __str__(self):
return self.title2.4. Create and Run Migrations
python manage.py makemigrations
python manage.py migrate2.5. Build a Serializer
# api/serializers.py
from rest_framework import serializers
from .models import Task
class TaskSerializer(serializers.ModelSerializer):
class Meta:
model = Task
fields = '__all__'2.6. Build API Views
Use DRF’s generic views for simplicity:
# api/views.py
from rest_framework import generics
from .models import Task
from .serializers import TaskSerializer
class TaskListCreate(generics.ListCreateAPIView):
queryset = Task.objects.all()
serializer_class = TaskSerializer
class TaskDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Task.objects.all()
serializer_class = TaskSerializer2.7. Create URLs
# api/urls.py
from django.urls import path
from .views import TaskListCreate, TaskDetail
urlpatterns = [
path('tasks/', TaskListCreate.as_view(), name='task-list'),
path('tasks/<int:pk>/', TaskDetail.as_view(), name='task-detail'),
]Include these in your project’s urls.py:
from django.urls import path, include
urlpatterns = [
# other paths...
path('api/', include('api.urls')),
]Step 3: Setting Up React
3.1. Create a React Project
In a sibling directory:
npx create-react-app frontend
cd frontend3.2. Install Axios
For API requests:
npm install axiosStep 4: Connect React to Django API
4.1. Example: Fetch and Display Tasks
// src/App.js
import React, { useEffect, useState } from "react";
import axios from "axios";
function App() {
const [tasks, setTasks] = useState([]);
useEffect(() => {
axios.get("http://localhost:8000/api/tasks/")
.then(response => setTasks(response.data));
}, []);
return (
<div>
<h1>Task List</h1>
<ul>
{tasks.map(task => (
<li key={task.id}>{task.title} - {task.completed ? "✔" : "❌"}</li>
))}
</ul>
</div>
);
}
export default App;4.2. Handle CORS
DRF will block requests from a different origin by default. Mention the need for django-cors-headers:
pip install django-cors-headersAdd 'corsheaders' to INSTALLED_APPS and set allowed origins in settings.py:
CORS_ALLOWED_ORIGINS = [
'http://localhost:3000',
]Step 5: Running and Testing
- Start Django:
python manage.py runserver - Start React:
npm start
Visit http://localhost:3000, and see your React app consuming data from Django!
Conclusion
- Django and React provide a clear, maintainable separation of backend and frontend, enabling teams to work independently.
- DRF makes API development delightful, and React enables interactive UIs that today’s users expect.
- With this stack, you’re set up for both rapid prototyping and scaling to production.
