Source code for django_htmx_messages.middleware

from django.contrib.messages import get_messages
from django.http import HttpRequest, HttpResponse
from django.template.loader import render_to_string
from django.utils.deprecation import MiddlewareMixin


[docs] class HtmxMessageMiddleware(MiddlewareMixin): """ Middleware that handles Django messages in HTMX responses. Middleware that moves messages into the HX-Trigger header when request is made with HTMX. """
[docs] def process_response( self, request: HttpRequest, response: HttpResponse ) -> HttpResponse: """ Process HTMX responses by injecting Django messages as toast notifications. Args: request: The HTTP request response: The HTTP response Returns: The modified response with injected messages """ # The HX-Request header indicates that the request was made with HTMX if "HX-Request" not in request.headers: return response # Ignore HTTP redirections because HTMX cannot read the body if 300 <= response.status_code < 400: return response # Ignore client-side redirection because HTMX drops OOB swaps if "HX-Redirect" in response.headers: return response # Extract the messages messages = get_messages(request) if not messages: return response response.write( render_to_string( template_name="toasts.html", context={"messages": messages}, request=request, ) ) return response