Follow these four steps to set up a basic integration:
- Configure Django: Add
'django_vite_plugin' to your INSTALLED_APPS in settings.py. - Configure Vite: Create a
vite.config.js and add djangoVitePlugin to the plugins array, passing your entry points (JS and CSS files). - Use in Templates: Load the
vite template tag and use {% vite %} to load the HMR client, then {% vite 'path/to/asset' %} for your specific files. - Run Servers: Run
python manage.py runserver in one terminal and npm run dev in another.
# 1. settings.py
INSTALLED_APPS = [
# ...
'django_vite_plugin',
]
// 2. vite.config.js
import { defineConfig } from 'vite'
import { djangoVitePlugin } from 'django-vite-plugin'
export default defineConfig({
plugins: [
djangoVitePlugin([
'myapp/js/main.js',
'myapp/css/styles.css',
])
],
})
{# 3. template.html #}
{% load vite %}
<!DOCTYPE html>
<html lang="en">
<head>
{% vite %}
{% vite 'myapp/js/main.js' 'myapp/css/styles.css' %}
</head>
<body>
</body>
</html>
# 4. Terminal commands
python manage.py runserver
npm run dev