views:

32

answers:

1

My code in the view:

   from django.contrib import messages
   messages.add_message(request, messages.INFO, 'Hello world.')

I don't want to show this code to the user the second time if he/she refreshes again. How do I go about doing that? Messages don't seem to have any sort of expiry setting. There is documentation here:

http://docs.djangoproject.com/en/1.2/ref/contrib/messages/#expiration-of-messages

+2  A: 

Messages are cleared as soon as you iterate messages (which should be available from RequestContext).

So step one is making sure you're displaying messages! If you want to hold-off on displaying messages for a certain page, you'll perhaps want to investigate punching things into session but it's getting a bit messy for my liking. I can't think of any good reasons to delay the message for the next pageload.

If you are displaying them and your messages are hanging on longer than you expect, perhaps you're re-adding them after they're being displayed. Try putting timestamps into your messages and you'll see if when they were written.

Oli
Makes sense. So Maybe in my view, I should store a session variable for first time users on a page and add the message only if the user has loaded the page for the first time. In that scenario, the user doesnt see the message when the page is refreshed. Correct me if there is a better way! Thanks.
ninja123