Skip to main content

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

  1. React/Redux/Typescript (Javascript not recommended)
  2. Vite is used for bundling
  3. Tailwind CSS is used for styling
  4. Quite opinionated but loosely coupled. The contents matter, structure doesn't

Run The Script

  1. You need to have node and a preferred dependency manager installed
  2. Create a new virtual environment using uv venv .venv or equivalent
  3. Install the package with uv pip install django-react-tailwind-vite or equivalent
  4. Run the script with dj-vite, this will configure vite, redux and the react app
  5. You can uninstall the package once you've verified successful installation

Post Script Instructions

  1. Install frontend packages by running pnpm install or equivalent
  2. Install the python dependencies by running uv pip install -r requirements.txt or equivalent
  3. 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:

  1. 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

  1. Run pnpm run dev and ./manage.py runserver in separate terminal windows
  2. Navigate to http://127.0.0.1:8000 and you will see the react home page loaded
  • Always use that url instead of localhost so that you use session auth
  1. Try changing contents in frontend/src/pages/home.tsx to see live reload in action
  2. The django urls connects to the react home via the app/* regex wildcard
  3. Build on from there

Production

  1. Ensure you set the DJANGO_VITE["default"]["dev_mode"] to False
  2. Run pnpm run build
  3. Run ./manage.py collectstatic
  4. Deploy your app and django-vite will auto serve bundled files

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_react_tailwind_vite-0.1.5.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_react_tailwind_vite-0.1.5-py3-none-any.whl (22.8 kB view details)

Uploaded Python 3

File details

Details for the file django_react_tailwind_vite-0.1.5.tar.gz.

File metadata

File hashes

Hashes for django_react_tailwind_vite-0.1.5.tar.gz
Algorithm Hash digest
SHA256 86b349fa02eadc192cd7b283e7652ee97e9bd343f36bfad6f3f63568b9298ef8
MD5 1a7cdc6bf5e26cf6e94134998f0e1220
BLAKE2b-256 b22b7b4b82af47c57175bcdc991233df24900d74d0fd20ded6f22f86fd1b906d

See more details on using hashes here.

File details

Details for the file django_react_tailwind_vite-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for django_react_tailwind_vite-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 73ecafb5192431bf910fdc74f4cae0ae4dbb02dc63337eabeaee0a387612aa48
MD5 1d9527623a921acd2abf921028c63a18
BLAKE2b-256 01fca002c6bca58307cef6b60f5632e91894d3988794085dd147591a12028413

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page