This project demonstrates how to pre-render React components on the server side and integrate them into Django templates. This approach combines the best of both worlds: React's component-based architecture and Django's robust templating system.
The project showcases a practical implementation of server-side React rendering that can be integrated into Django templates. This is particularly useful for:
- Improving initial page load performance
- Better SEO (Search Engine Optimization)
- Progressive enhancement of web applications
-
Python Environment Setup
# Create and activate virtual environment python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate # Install Django dependencies pip install django
-
Node.js Setup
# Install Node.js dependencies cd django_app npm install
-
Build Process
# Build React components npm run build -
Run Django Server
python manage.py runserver
- React components are written in the
static/jsdirectory - The pre-rendering script (
prerender.js) renders these components to static HTML - The generated HTML is stored in
static/dist/prerendered.html - Django templates include this pre-rendered HTML
- The page loads with the pre-rendered content, then hydrates with React for interactivity
- Server-side rendering of React components
- Seamless integration with Django templates
- Build process for generating static HTML
- Progressive enhancement approach
- Django
- React 18
- Node.js
- Babel for transpilation
To modify the React components:
- Edit the components in
static/js - Run
npm run buildto generate new pre-rendered HTML - Refresh your Django page to see changes
MIT License