Htmx
hypha.core.middleware.htmx
¶
HtmxMessageMiddleware
¶
Bases: MiddlewareMixin
Middleware that transfers Django messages into the HX-Trigger header for HTMX requests.
This middleware captures Django messages and adds them to the HX-Trigger header when the request is made with HTMX. It preserves any existing HX-Trigger values and adds the messages in a standardized format that can be processed by frontend JavaScript.
The messages are formatted as an array of objects, each containing: - message: The text content of the message - tags: CSS class names or other categorization for the message
process_response
¶
Source code in hypha/core/middleware/htmx.py
HtmxAuthRedirectMiddleware
¶
Middleware to handle HTMX authentication redirects properly.
When an HTMX request results in a 302 redirect (typically for authentication), this middleware: 1. Changes the response status code to 204 (No Content) 2. Adds an HX-Redirect header with the redirect URL 3. Preserves the original request path in the 'next' query parameter
This ensures that after authentication, the user is returned to the page they were attempting to access, maintaining a seamless UX with HTMX.
Credits: https://www.caktusgroup.com/blog/2022/11/11/how-handle-django-login-redirects-htmx/