I have happily just discovered the messages framework. I used to add notifications to my context dict, but I started using allauth and saw how it used messages, and I can see how it makes some notifications easier to manage.
However, when I started using my own messages, I sometimes see other messages in my template if I simply loop over messages. For example if I log in and then go to a page that uses my own message, I see my own message and an older message that the login was successful. I believe allauth adds a message about the successful login, but since I redirect to the home page on login that message never gets rendered and stays on the queue.
I have read a number of SO posts and documentation pages about this, but it’s still not clear to me how I should be clearing the queue before adding my own messages. Also, is there a reason why there’s no messages.clear() method? It seems like it would be useful in a range of situations, but maybe there’s some complexity I’m missing?
Interesting topic - I’ve never considered this before, so I’ve learned a lot in the process of reading about it. Thanks for raising the question!
Just to toss out some ideas, I can see a couple different options here:
Change your home page template to display messages. (There are many reasons why you might want to do this and a couple as to why you might not.)
If you’re always going to do this immediately prior to adding your own messages, something like messages.get_messages(request).used = True may be sufficient. (My initial reading of the source code is that the used attribute takes effect when new messages are added - I could be reading this wrong, but that’s my first/current impression. If I’m right, then doing this right before you add your messages should give you a clean slate.)
If you want to be more comprehensive, you could subclass the message backend that you are currently using, add a clear() method to it, and use it as your message backend. It’s not the choice I would make, but it is an option.
For my own edification I tried digging through the Django tickets and the Django Developers mailing list to see if there were a ticket filed for this or any other discussion, and couldn’t find one. While the Q&As on SO are a clear indication that some people have been interested in the topic, it doesn’t appear to be enough of a pain-point for anyone to propose a patch.
I was working on a project the other day that clears messages, via a custom function that loops over them without doing anything. I can see it would be useful to add this to Django, it wouldn’t be a big patch. Would you like to open a ticket @ehmatthes?
1 - I thought about iterating over the messages in the home page template, but at this point the only reason to do that is to clear the messages for the view I’m working on. And that feels really disconnected and clunky.
2 - I tried this, and it didn’t clear the login success messages.
For the moment, I used the extra_tags parameter to add a tag specific to this use case, and then only display messages that have this extra tag.
I would be happy to open a ticket, but I won’t get to it right away. I keep a tag in my projects called upstream_contribution, and when I get time I try to go back to those issues. I’ll note this in an issue in my project, and try to get to it before too long.
For the record I tried looping over messages in my view function before adding my own message, but that didn’t work. I am not sure why at this point. I ended up using the extra_tags parameter, as described above.
I ran into this fairly recently too on my Twitch stream. My solution was to clear cookies, but I’m not sure I’d recommend it.
The other option that I ended up using elsewhere was to make a block inside my base template where I do the looping over messages. When I don’t want messages to appear, I override the block in the individual template and leave it empty. It’s not the most elegant solution, per se, but it does consume all the messages.
What I noticed is that the Django message is automatically cleared when you iterate. With my environment, I use sweet alert to show errors in my base file. So what I did is at the end of my base file, I created a script tag, created a variable mess, looped through messages, and appended each error message to the mess variable, After the loop, I use my js function to show the error message. my code example is below