middleware - Django-Components" > middleware - Django-Components" >
Skip to content

middleware ¤

ComponentDependencyMiddleware ¤

ComponentDependencyMiddleware(get_response: Callable[[HttpRequest], HttpResponse])

Middleware that inserts CSS/JS dependencies for all rendered components at points marked with template tags.

Source code in src/django_components/middleware.py
def __init__(self, get_response: "Callable[[HttpRequest], HttpResponse]") -> None:
    self.get_response = get_response

    if iscoroutinefunction(self.get_response):
        markcoroutinefunction(self)

DependencyReplacer ¤

DependencyReplacer(css_string: bytes, js_string: bytes)

Replacer for use in re.sub that replaces the first placeholder CSS and JS tags it encounters and removes any subsequent ones.

Source code in src/django_components/middleware.py
def __init__(self, css_string: bytes, js_string: bytes) -> None:
    self.js_string = js_string
    self.css_string = css_string

join_media ¤

join_media(components: Iterable[Component]) -> Media

Return combined media object for iterable of components.

Source code in src/django_components/middleware.py
def join_media(components: Iterable["Component"]) -> Media:
    """Return combined media object for iterable of components."""

    return sum([component.media for component in components], Media())