Script to set up a Django-React hybrid project with Tailwind CSS, and Vite.
Project description
Hybrid Django-React-Tailwind-Vite Setup
Script to bootstrap hybrid django-react projects set up inspired by
- The hybrid Python/Django/React Architecture as described by Cory Zue in this article
- Session based Auth for SPA/Django as described by Nik Tomazic in this article
- The benefits of this set up is that you're able to use django features where you want and selectively use React. Also you don't have to worry about JWT as we're using normal django authentication.
Quick Overview
- React/Redux/Typescript (Javascript not recommended)
- Vite is used for bundling
- Tailwind CSS is used for styling
- Quite opinionated but loosely coupled. The contents matter, structure doesn't
Run The Script
- You need to have node and a preferred dependency manager installed
- Run the script
python3 set_up_frontend.py - This will configure vite, redux and the react app and install all dependecies
Post Script Instructions
- Install frontend packages by running
pnpm installor equivalent - Install the python dependencies by running
uv pip install -r requirements.txtor equivalent - The django project settings file has the following additions
#TEMPLATES["DIRS"] list in project/settings.py import os # top of file os.path.join(BASE_DIR, "templates") #static files section STATIC_ROOT = os.path.join(BASE_DIR.parent, "static") STATICFILES_DIRS = [ os.path.join(BASE_DIR, "assets"), ] DJANGO_VITE = { "default": { "dev_mode": DEBUG, #to serve contents dynamically "manifest_path": os.path.join(BASE_DIR, "assets", "manifest.json"), } } #Allow Session Auth For React SPA CSRF_COOKIE_SAMESITE = "Lax" SESSION_COOKIE_SAMESITE = "Lax" CSRF_COOKIE_HTTPONLY = False # False since we will grab it via universal-cookies SESSION_COOKIE_HTTPONLY = True SESSION_COOKIE_AGE = 60 * 60 * 24 * 7 # 1 week
To further understand these values, read:
- For Making API Queries to your backend, use this snippet in your axios client
import axios from "axios";
import Cookies from "universal-cookie";
const cookies = new Cookies();
async function restClient(url: string, method: string, data: any) {
const response = await axios({
url: url,
method: method,
data: data,
headers: {
"Content-Type": "application/json",
"X-CSRFToken": cookies.get("csrftoken"),
},
withCredentials: true,
});
return response;
}
The idea is to have csrf token as part of your headers. That's why you don't need JWT!
Start Your Project
- Run
pnpm run devand./manage.py runserverin separate terminal windows - Navigate to
http://127.0.0.1:8000and you will see the react home page loaded
- Always use that url instead of localhost so that you use session auth
- Try changing contents in
frontend/src/pages/home.tsxto see live reload in action - The django urls connects to the react home via the
app/*regex wildcard - Build on from there
Production
- Ensure you set the
DJANGO_VITE["default"]["dev_mode"]toFalse - Run
pnpm run build - Run
./manage.py collectstatic - Deploy your app and django-vite will auto serve bundled files
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_react_tailwind_vite-0.1.1.tar.gz.
File metadata
- Download URL: django_react_tailwind_vite-0.1.1.tar.gz
- Upload date:
- Size: 13.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6955c581cf10c8e6ab7e5b0941271cb796d8b9b6090b74a268dd39d934865bba
|
|
| MD5 |
b0d693ce3d1e22c3e0fd723226ee7795
|
|
| BLAKE2b-256 |
e348cd650cc20f919fb3b0e824c0bd42a33843e48e5316a5ca16a150545cd54f
|
File details
Details for the file django_react_tailwind_vite-0.1.1-py3-none-any.whl.
File metadata
- Download URL: django_react_tailwind_vite-0.1.1-py3-none-any.whl
- Upload date:
- Size: 22.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
223e17acb0fddce6a7c87bc4a69fa814829a68e368bb0d0d880d47f606fea86b
|
|
| MD5 |
02d2731775a2a3d35bcc241d530242e4
|
|
| BLAKE2b-256 |
2f13b0ee5b3290df79961f8bc93fd5c04fda116a1ff508bfebb9f9c526e6199d
|