**everything you need to know** to **create a full-stack project using Django (with Django REST Framework) and React**, including setup, project structure, communication between backend and frontend, deployment, and best practices.
Hereβs
---
# π§ Full-Stack Django + DRF + React Cheat Sheet
---
## π§± 1. **Tech Stack Breakdown**
| Layer | Technology | Purpose |
| ------------ | --------------------------------------------- | ------------------------- |
| Backend | Django + Django REST Framework | API + ORM + Auth |
| Frontend | React (with Vite or Next.js) | SPA / UI |
| API Format | JSON (via DRF) | Communicate between FE/BE |
| Auth Options | JWT / Session | Token-based auth |
| Deployment | Docker / Gunicorn + Nginx / Vite static build | Production hosting |
---
## π¦ 2. **Backend Setup (Django + DRF)**
### π Project Structure
```/
myproject/
βββ backend
β βββ manage.py/ # Django settings
β βββ myproject/ # Django app for your API
β βββ api
β βββ models.py
β βββ serializers.py
β βββ views.py
β βββ urls.py/ # React app
βββ frontend
β βββ package.json/
β βββ src
```
### π§ͺ Create Backend
```bash# Create and activate virtualenv
-m venv .venv
python /bin/activate
source .venv
# Install Django + DRF
-cors-headers
pip install django djangorestframework djoser django
# Create project and app
-admin startproject myproject backend
django
cd backend
python manage.py startapp api
```
### βοΈ Update `settings.py`
```python= [
INSTALLED_APPS
...'rest_framework',
'corsheaders',
'api',
]
= [
MIDDLEWARE 'corsheaders.middleware.CorsMiddleware',
...
]
= [
CORS_ALLOWED_ORIGINS "http://localhost:5173", # or Vite dev server
]
= {
REST_FRAMEWORK 'DEFAULT_PERMISSION_CLASSES': ['rest_framework.permissions.AllowAny'],
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
],
}
```
### π Sample API Setup
#### `models.py`
```pythonfrom django.db import models
class Todo(models.Model):
= models.CharField(max_length=100)
title = models.BooleanField(default=False)
completed
```
#### `serializers.py`
```pythonfrom rest_framework import serializers
from .models import Todo
class TodoSerializer(serializers.ModelSerializer):
class Meta:
= Todo
model = '__all__'
fields
```
#### `views.py`
```pythonfrom rest_framework import viewsets
from .models import Todo
from .serializers import TodoSerializer
class TodoViewSet(viewsets.ModelViewSet):
= Todo.objects.all()
queryset = TodoSerializer
serializer_class
```
#### `urls.py`
```pythonfrom rest_framework.routers import DefaultRouter
from .views import TodoViewSet
= DefaultRouter()
router r'todos', TodoViewSet)
router.register(
= router.urls
urlpatterns
```
#### `backend/urls.py`
```pythonfrom django.urls import path, include
= [
urlpatterns 'api/', include('api.urls')),
path(
]
```
---
## βοΈ 3. **Frontend Setup (React + Vite)**
```bash# From project root
@latest frontend -- --template react-ts
npm create vite
cd frontend
npm install
```
### π§° Useful Packages
```bash-router-dom
npm install axios react
```
### π Example Project Structure
```/
frontend/
βββ src/
β βββ pages
β β βββ TodoPage.tsx/
β βββ components
β βββ App.tsx
β βββ main.tsx
```
### π Axios Setup
#### `api/axios.ts`
```tsimport axios from 'axios';
= axios.create({
const api 'http://localhost:8000/api/',
baseURL: ;
})
;
export default api
```
---
## π 4. **Sample React Page**
#### `pages/TodoPage.tsx`
```tsximport React, { useEffect, useState } from 'react';
import api from '../api/axios';
type Todo = {
id: number;
;
title: string;
completed: boolean;
}
export default function TodoPage() {= useState<Todo[]>([]);
const [todos, setTodos]
=> {
useEffect(() <Todo[]>('todos/').then((res) => setTodos(res.data));
api.get;
}, [])
return (
<div>
<h2>Todo List</h2>
<ul>
map((todo) => (
{todos.<li key={todo.id}>{todo.title}</li>
))}</ul>
</div>
;
)
}
```
---
## π 5. **Authentication (Optional)**
### Use `djoser` or JWT
```bash-simplejwt
pip install djangorestframework
```
In `settings.py`:
```python= {
REST_FRAMEWORK 'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
)
}
```
in frontend, use:
Then
```tsx= async () => {
const login = await api.post("token/", { username, password });
const res "access", res.data.access);
localStorage.setItem(;
}
```
---
## π¦ 6. **Deployment Overview**
| Layer | Tool |
| -------- | ------------------------------- |
| Backend | Gunicorn + Nginx + PostgreSQL |
| Frontend | Build with Vite β `dist/` |
| Hosting | Render, Heroku, Railway, Docker |
```bash# Build frontend
cd frontend
npm run build
# Copy dist to Django
/backend/frontend_build/
mv dist ..
# Serve with Django staticfiles or Nginx
```
---
## β
7. **Best Practices**
| Area | Best Practice |
| ----------- | -------------------------------------------------- |
| CORS | Use `django-cors-headers` in dev, stricter in prod |
| Env vars | Use `.env` + `python-decouple` or `django-environ` |
| Security | Use HTTPS, CSRF token if needed |
| Performance | Use pagination, limit fields in serializers |
| API Design | RESTful endpoints, versioning (`/api/v1/`) |
---
## π Summary Diagram
```
Frontend (React)
β axios/api/`
API Gateway: `
β
Backend (Django DRF)/ SQLite)
β³ DB (PostgreSQL
```
---
**GitHub starter template** or **Docker setup** for this stack? Would you like a
React and Django
React and Django