Middleware assistance for Django messages?
See original GitHub issueDescription
I’d like to start using htmx (means I’m an htmx newbie). Is there a recommended way of handling messages created in views with code such as messages.success(request, 'All good')
?
Perhaps HtmxMiddleware
could check request._messages._queued_messages
, and if that’s set, add a HX-Trigger
response header to the response, and this could cause the client to make a request back to the server to collect the messages.
https://github.com/django/django/blob/main/django/contrib/messages/middleware.py#L22 https://github.com/django/django/blob/main/django/contrib/messages/storage/base.py#L149
Might also be possible to do it this way:
https://docs.djangoproject.com/en/dev/ref/contrib/messages/#expiration-of-messages
I don’t know what I’m talking about. I just would like Django messages to appear, and if django-htmx can assist this in a nice way…
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:16 (7 by maintainers)
Top GitHub Comments
@CapnKernel Follow this great tutorial with Toasts and Django https://blog.benoitblanchon.fr/django-htmx-toasts/
Btw toast messages are pretty tricky accessibility-wise, for example see this post that summarizes how to meet a11y criteria when using them. They suggest a minimum delay of 6 seconds, based on message length. Personally I’ve decided to stick to messages the user has to deliberately close, or navigate away from, rather than using a timer.