How to make django messages StackOverflow style?
Asked Answered
T

4

11

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!

Tiresome answered 19/6, 2010 at 16:52 Comment(0)
S
8

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.

  • It's a fairly trivial task to implement
  • a model with a foreign key on User
  • a context processor to have the messages available in the templates
  • a view to consume a message
  • maybe a helper function to create the messages

Don't forget to make it available to others if you do so =)

Sunday answered 21/6, 2010 at 11:37 Comment(4)
Currently there is 5 applications providing somewhat messages-notices feature: code.djangoproject.com/wiki/SessionMessagesBiblicist
Thanks @Guillaume Esquevin, this seems like the best approach for messages tied to users instead of sessions.Lessard
A newbie question - why do I need a middleware class? As far as I can tell, the context_processor does all the hooking work or am I wrong?Tiresome
You are right, the middleware is not needed there, you can simply fetch the user's messages in the context processor. It's only for temporary messages that a middleware is needed in order to instantiate an array to put messages in. I'll edit my post.Sunday
B
7

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.

Biblicist answered 21/6, 2010 at 9:32 Comment(1)
Is there any replacement that is tied to the users module? I.e., I'd like messages to always appear for the user, and not be tied to a particular session.Lessard
C
4

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.

Canna answered 23/2, 2011 at 14:26 Comment(0)
N
2

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.

Noodlehead answered 19/6, 2010 at 17:50 Comment(1)
Since 1.2, Django has a new messages framework apart from the old API. It is as stable as any other contrib component.Biblicist

© 2022 - 2024 — McMap. All rights reserved.