Integration of Vite in a Django project.
npx @tessl/cli install tessl/pypi-django-vite@3.1.0Integration of ViteJS (a modern frontend build tool) and Django web framework, enabling developers to use Vite's fast development server, hot module replacement (HMR), and optimized production builds within Django projects.
pip install django-vitefrom django_vite import DjangoViteConfigFor Django template usage:
# In templates, load the template tags
{% load django_vite %}For programmatic asset loading:
from django_vite.core.asset_loader import DjangoViteAssetLoaderFor tag generation utilities:
from django_vite.core.tag_generator import TagGenerator, attrs_to_strFor exception handling:
from django_vite.core.exceptions import (
DjangoViteManifestError,
DjangoViteAssetNotFoundError,
DjangoViteConfigNotFoundError
)# settings.py
INSTALLED_APPS = [
# ... other apps
'django_vite',
# ... your apps that use django-vite
]
# Basic configuration
DJANGO_VITE = {
"default": {
"dev_mode": True, # Set to False for production
"dev_server_port": 5173,
"static_url_prefix": "",
}
}<!-- templates/base.html -->
{% load django_vite %}
<!DOCTYPE html>
<html>
<head>
<title>My Django App</title>
<!-- Include Vite assets -->
{% vite_asset 'src/main.js' %}
</head>
<body>
<!-- HMR client for development -->
{% vite_hmr_client %}
<!-- React refresh for development -->
{% vite_react_refresh %}
<!-- Your content -->
</body>
</html>In development mode (dev_mode: True):
In production mode (dev_mode: False):
Django-vite follows a modular architecture:
This design supports multi-app configurations where different parts of a Django project can use separate Vite configurations while sharing the same asset loading infrastructure.
Configuration system supporting both single-app and multi-app Vite setups with development and production modes.
class DjangoViteConfig(NamedTuple):
dev_mode: bool = False
dev_server_protocol: str = "http"
dev_server_host: str = "localhost"
dev_server_port: int = 5173
static_url_prefix: str = ""
manifest_path: Optional[Union[Path, str]] = None
legacy_polyfills_motif: str = "legacy-polyfills"
ws_client_url: str = "@vite/client"
react_refresh_url: str = "@react-refresh"
app_client_class: str = "django_vite.core.asset_loader.DjangoViteAppClient"Django template tags for including Vite assets, HMR client, React refresh, and legacy browser support in templates.
def vite_asset(path: str, app: str = "default", **kwargs) -> str: ...
def vite_asset_url(path: str, app: str = "default") -> str: ...
def vite_preload_asset(path: str, app: str = "default") -> str: ...
def vite_hmr_client(app: str = "default", **kwargs) -> str: ...
def vite_react_refresh(app: str = "default", **kwargs) -> str: ...
def vite_legacy_polyfills(app: str = "default", nomodule: bool = True, **kwargs) -> str: ...
def vite_legacy_asset(path: str, app: str = "default", **kwargs) -> str: ...Core asset loading system with manifest parsing, URL generation, and programmatic asset management.
class DjangoViteAssetLoader:
@classmethod
def instance(cls) -> 'DjangoViteAssetLoader': ...
def generate_vite_asset(self, path: str, app: str = "default", **kwargs) -> str: ...
def generate_vite_asset_url(self, path: str, app: str = "default") -> str: ...
def preload_vite_asset(self, path: str, app: str = "default") -> str: ...
def check(self, **kwargs) -> List[Warning]: ...
class DjangoViteAppClient:
def __init__(self, config: DjangoViteConfig, app_name: str = "default"): ...
def generate_vite_asset(self, path: str, **kwargs) -> str: ...
def get_dev_server_url(self, path: str) -> str: ...
def get_production_server_url(self, path: str) -> str: ...
class ManifestClient:
def __init__(self, config: DjangoViteConfig, app_name: str = "default"): ...
def get(self, path: str) -> ManifestEntry: ...
def check(self) -> List[Warning]: ...Low-level HTML tag generation utilities used by the asset loading system.
class TagGenerator:
@staticmethod
def script(src: str, attrs: Dict[str, str]) -> str: ...
@staticmethod
def stylesheet(href: str, attrs: Optional[Dict[str, str]] = None) -> str: ...
@staticmethod
def stylesheet_preload(href: str, attrs: Optional[Dict[str, str]] = None) -> str: ...
@staticmethod
def preload(href: str, attrs: Dict[str, str]) -> str: ...
def attrs_to_str(attrs: Dict[str, str]) -> str: ...Django app configuration and system checks for django-vite integration.
class DjangoViteAppConfig(AppConfig):
name: str = "django_vite"
verbose_name: str = "Django Vite"
def ready(self) -> None: ...
def check_loader_instance(**kwargs) -> List[Warning]:
"""Django system check function for validating configurations."""class DjangoViteManifestError(RuntimeError):
"""Manifest parsing failed."""
class DjangoViteAssetNotFoundError(RuntimeError):
"""Vite Asset could not be found."""
class DjangoViteConfigNotFoundError(RuntimeError):
"""DjangoViteConfig not found in DJANGO_VITE settings."""class ManifestEntry(NamedTuple):
file: str
src: Optional[str] = None
isEntry: Optional[bool] = False
isDynamicEntry: Optional[bool] = False
css: Optional[List[str]] = []
imports: Optional[List[str]] = []
dynamicImports: Optional[List[str]] = []