I recently realized that a module in our Django web app was using django.contrib.messages. However, the template's context processor did not have the django.contrib.messages.context_processors.messages
processor added.
I'm worried that when I push this to production, users will see all their old messages for all pages that had generated them. Is there a way to clear these messages for all users from the django shell
?
Messages will be shown for those users whose sessions already contain the message. So you have several options to do.
In case you're using the default settings and session data are stored in a database table, you can iterate through active sessions and check to see if there's any message stored for them.
In case you don't use the default settings and store session data elsewhere (Redis?), or you don't want to go the previous way, you can drop all sessions altogether. This will have a drawback, that your users will be logged out (which might be an acceptable consequence)
You can do this per each request of your users, you need to delete session's messages without deleting all the session object. Something like the following would do:
or remove them using the prefered django-ish way:
The drawback of this method is that you have to only remove messages, if its the first visit of an already logged in user, and then don't do it again, since you want your users to be able to view their messages from now on.
Messages are usually stored either in sessions or cookies (check your storage backend). You can clear them from there, but they'll be cleared when sessions/cookies are normally cleared anyway (on user logout at the worst).
If you want to be sure these messages don't display, you can disable the messaging framework: