question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Middleware assistance for Django messages?

See original GitHub issue

Description

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:open
  • Created 2 years ago
  • Reactions:3
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
ericmuijscommented, Nov 27, 2022

@CapnKernel Follow this great tutorial with Toasts and Django https://blog.benoitblanchon.fr/django-htmx-toasts/

1reaction
adamchainzcommented, Sep 13, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The messages framework | Django documentation
For this, Django provides full support for cookie- and session-based messaging, ... Messages are implemented through a middleware class and corresponding ...
Read more >
Django Middleware flash messages in view methods
Django Middleware flash messages in view methods: Django built-in flash messages, Add flash messages, Techniques to add Django flash messages, ...
Read more >
Django messages framework, custom middleware and ...
Messages are associated with a user via the session. But the session ID is stored in a cookie - and cookies are domain-specific....
Read more >
Django tutorial for beginners - 56 Middleware for ... - YouTube
Django tutorial for beginners - 56 Middleware for Marketing Message · Key moments. View all · Key moments · Description · Key moments....
Read more >
Django+HTMX: integration with the messages framework
Use Django's messages framework transparently with HTMX.Every message generated in the Django backend will be rendered as a Bootstrap toast.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found