views:

232

answers:

3

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!

+2  A: 

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.

Dave W. Smith
Since 1.2, Django has a new messages framework apart from the old API. It is as stable as any other contrib component.
Török Gábor
+4  A: 

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.

Török Gábor
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.
Edan Maor
+4  A: 

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 =)

Guillaume Esquevin
Currently there is 5 applications providing somewhat messages-notices feature: http://code.djangoproject.com/wiki/SessionMessages
Török Gábor
Thanks @Guillaume Esquevin, this seems like the best approach for messages tied to users instead of sessions.
Edan Maor
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?
Yuval Cohen
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.
Guillaume Esquevin