django-components/docs/user_guide/creating_using_components/middleware.md
2024-08-24 11:23:24 +02:00

804 B

Setting Up ComponentDependencyMiddleware

[ComponentDependencyMiddleware][django_components.middleware.ComponentDependencyMiddleware] is a Django middleware designed to manage and inject CSS/JS dependencies for rendered components dynamically. It ensures that only the necessary stylesheets and scripts are loaded in your HTML responses, based on the components used in your Django templates.

To set it up, add the middleware to your [MIDDLEWARE][] in settings.py:

MIDDLEWARE = [
    # ... other middleware classes ...
    'django_components.middleware.ComponentDependencyMiddleware'
    # ... other middleware classes ...
]

Then, enable RENDER_DEPENDENCIES in setting.py:

COMPONENTS = {
    "RENDER_DEPENDENCIES": True,
    # ... other component settings ...
}