I'd like to use Django's Messages module, however, I would like my messages to persist until the user clicks an X next to the message as opposed to having messages disappear as soon as the user reloads the page.
I am stumped with two issues: How do I make the messages' context processor not delete the messages once they are accessed? How do I later on delete the message from the DB explicitly once the user clicks on the "remove" button (which invokes an ajax call)?
Thanks!
In your case django.contrib.messages
won't bring you anywhere good. It's a message system inspired by RoR flash system, where messages aren't supposed to stay around
You should create your own messaging system (django-persistent-messages maybe?) that would save messages for registered users in database.
Don't forget to make it available to others if you do so =)
Since 1.2, Django has a new messages framework--django.contrib.messages
--that is now completely detached from the auth
module and offers much more functionality. For instance, it provides a basic way of handling the expiration of messages.
You can also take a look at the django-cnotes application that provides a simple cookie based user notification system. Setting constant CNOTES_AUTO_CLEAR
to False
prevents clearing the notes automatically.
And there is django-notices, yet another replacement for the built-in message notification system. It does no magic but provides an elegant and simple API.
Since late 2010, there is the library, django-persistent-messages, for exactly this goal. It's reasonably well documented and works well to create a Stack Overflow-style messaging system.
It also integrates with the built-in Django messaging system, so the code changes are relatively minor, and you can still use the original system for messages that don't need to be persistent.
Django messages might seem like a good starting point, but require contortions to get to where you want to go, and I wouldn't trust that future release of Django wouldn't break your hacks.
Implementing your own UserMessage model will probably serve you better in the long run. That gives you complete, unambiguous control over the message lifecycle. It might make a nice reusable app, too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With