Building modern web applications requires a balance between backend power and frontend efficiency. Django, a robust backend framework, and Next.js, a powerful React-based frontend framework, provide the perfect combination for scalable web apps.
First, install Django and Django REST framework:
pip install django djangorestframework
Create a new Django project:
django-admin startproject mybackend
cd mybackend
python manage.py startapp api
Configure settings.py
to include rest_framework
:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'api',
]
Define a basic Django REST Framework (DRF) API:
from rest_framework.decorators import api_view
from rest_framework.response import Response
@api_view(['GET'])
def hello_world(request):
return Response({"message": "Hello from Django!"})
Update urls.py
:
from django.urls import path
from .views import hello_world
urlpatterns = [
path('api/hello/', hello_world, name='hello-world')
]
Run the Django server:
python manage.py runserver
Create a new Next.js app:
npx create-next-app@latest myfrontend
cd myfrontend
npm install axios
Create a component to fetch and display data from Django:
import { useEffect, useState } from 'react';
import axios from 'axios';
export default function Home() {
const [message, setMessage] = useState('');
useEffect(() => {
axios.get('http://localhost:8000/api/hello/')
.then(response => setMessage(response.data.message))
.catch(error => console.error(error));
}, []);
return (
<div className="p-6 text-center">
<h1 className="text-2xl font-bold">{message}</h1>
</div>
);
}
Run the Next.js app:
npm run dev
pip install gunicorn
Run Gunicorn:
gunicorn mybackend.wsgi:application --bind 0.0.0.0:8000
npm run build
vercel
By combining Django’s robust backend with Next.js’s modern frontend capabilities, you can build scalable, high-performance web applications efficiently. Whether you're developing an e-commerce platform, SaaS application, or dashboard, this stack is a powerful choice.
💬 What are you building with Django and Next.js? Share your thoughts in the comments!