Django Vite

3.1.0 · active · verified Sat Apr 11

django-vite integrates Vite.js into Django projects, allowing for fast frontend development with Hot Module Replacement (HMR) and efficient production builds. It handles manifest file parsing and asset serving for both development and production environments. The current version is 3.1.0, with frequent patch releases and minor updates.

Warnings

Install

Imports

Quickstart

To integrate django-vite, first add 'django_vite' to `INSTALLED_APPS`. Then, configure `DJANGO_VITE` in your settings.py, specifying the `build_dir`, `manifest_path`, and `dev_server_url`. Ensure your Vite project builds its assets to the specified `build_dir`. Finally, include `{% vite_hmr_client %}` and `{% vite_asset 'your_entry_point.js' %}` in your Django templates.

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

INSTALLED_APPS = [
    # ... other apps
    'django_vite',
]

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') # For collectstatic

# Vite configuration
DJANGO_VITE = {
    'default': {
        'build_dir': os.path.join(BASE_DIR, 'static', '.vite'),
        'manifest_path': os.path.join(BASE_DIR, 'static', '.vite', 'manifest.json'),
        'dev_server_url': 'http://localhost:5173',
        'static_url_prefix': '/static/', # Must match STATIC_URL for production
    }
}

# In your template (e.g., templates/base.html):
# {% load django_vite %}
# <!DOCTYPE html>
# <html lang="en">
# <head>
#     <meta charset="UTF-8">
#     <meta name="viewport" content="width=device-width, initial-scale=1.0">
#     <title>My Django Vite App</title>
#     {% vite_hmr_client %}
#     {% vite_asset 'src/main.js' %}
# </head>
# <body>
#     <div id="app"></div>
# </body>
# </html>

view raw JSON →