π οΈ Scalable Django + Frontend Project Setup
Complete project directory GitHub Link
This guide helps you set up a decoupled Django REST backend and a separate frontend directory using vanilla JavaScript, allowing for clean team collaboration.
π Project Structure
project_name/
βββ backend/ β Django project and API app
βββ frontend/ β HTML/CSS/JS static frontend
βββ .venv/ β Python virtual environment
π’ Step 1: Set Up the Project Root
mkdir project_name
cd project_name
π’ Step 2: Create a Virtual Environment
python -m venv .venv
# For Linux/macOS:
source .venv/bin/activate
# For Windows CMD:
.venv\Scripts\activate
π’ Step 3: Install Required Packages
pip install djangorestframework django-cors-headers
pip freeze > requirements.txt
π
django-cors-headersallows your frontend and backend to communicate when served from different origins.
π’ Step 4: Create the Frontend and Backend Structure
mkdir frontend
django-admin startproject backend
cd backend
python manage.py startapp api
π’ Step 5: Configure Django Settings
Add the following to backend/settings.py:
INSTALLED_APPS = [
...
'rest_framework',
'corsheaders',
'api',
]
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
...
]
CORS_ALLOW_ALL_ORIGINS = True # For development only
π In production, use:
CORS_ALLOWED_ORIGINS = ["https://your-frontend-domain.com"]
π’ Step 6: Create a Sample API
π backend/api/views.py
from rest_framework.response import Response
from rest_framework.decorators import api_view
@api_view(['GET'])
def hello_world(request):
return Response({"message": "Hello from Django API!"})
π backend/api/urls.py
from django.urls import path
from .views import hello_world
urlpatterns = [
path('hello/', hello_world),
]
π backend/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('api.urls')),
]
π’ Step 7: Run the Backend Server
python manage.py runserver
If you see a message about unapplied migrations, run:
python manage.py migrate
python manage.py runserver
Visit: http://127.0.0.1:8000/api/hello/
β You should see something like:
{ "message": "Hello from Django API!" }
(Image 1: backend working properly)
π’ Step 8: Set Up the Frontend
π frontend/index.html
<!DOCTYPE html>
<html>
<head>
<title>Frontend</title>
</head>
<body>
<h1 id="message">Loading...</h1>
<script src="main.js"></script>
</body>
</html>
π frontend/main.js
fetch("http://127.0.0.1:8000/api/hello/")
.then(res => res.json())
.then(data => {
document.getElementById("message").textContent = data.message;
})
.catch(err => {
document.getElementById("message").textContent = "Error connecting to backend";
console.error(err);
});
βΆοΈ Serve the Frontend Locally
cd frontend
python -m http.server 5173
Visit: http://localhost:5173
β
You should see your message fetched from Djangoβs API.
β Final Result
- π’ Frontend running at: http://localhost:5173
- π’ Backend API running at: http://127.0.0.1:8000/api/hello/
- π Fully decoupled, modern architecture
- π₯ Easy for teams to collaborate without Django knowledge


Top comments (0)