I know this question sounds weird, but please, let me explain myself.
I'm using a decorator to make a message persist until the user actually dismisses it (like the behavior of stack overflow's messages). The problem is, as a result of this, the message gets added before the user signs out, and so the message gets displayed right after the user logs out. I'm wondering what the best way to remove the message in the logout view is. I've thought of two ways to do this, and am wondering if anyone can think of a better one.
I'm currently favoring this:
storage = messages.get_messages(request) storage.used = True
Over this:
storage = messages.get_messages(request) del storage._loaded_messages[0]
To me the second way seems more explicit, even though it is uglier: my intention is to remove the currently loaded messages and this makes that clear. The first way employs a means by which the messages will be cleared as a side effect ... but at least it doesn't rely upon a dunder variable ... what do you guys think?
clear() function. Show activity on this post. You need to iterate through the messages for your current set of messages as retrieved from the 'get_messages' method. You can use this code anywhere you want to clear the messages before setting new ones or where you want to simply clear all messages.
The messages framework allows you to temporarily store messages in one request and retrieve them for display in a subsequent request (usually the next one). Every message is tagged with a specific level that determines its priority (e.g., info , warning , or error ).
I like this simpler approach for clearing out the underlying iterator, since I actually wanted to add a new message in the place of a standard Django message.
list(messages.get_messages(request))
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