Search code examples
pythondjangomessage

How to prevent Django messages from leaking out to other modules?


I am currently using the built-in django-messages framework of django version 1.10.

However, since the messages are stored in the request, and therefore not "namespaced" as it were for different modules, I am concerned that this might lead to potential circumstances where messages created by one module (e.g. a messaging framework "your message has been sent") might bleed into another.

Is there a way to "namespace" these messages so we dont have this unintended affect?

In addition, the documentation says that messages expire if they are iterated over, does that mean that if I forget to iterate over them, they have the potential to build up over multiple requests?


Solution

  • You don't have to iterate over messages to expire them. Django does that for you.

    When one request gets a message it's iterated over with the next request, gets displayed if the template allows it and is removed from request data. That means it's shown once and is removed.

    The only way to get a message from your email module to be displayed in the account module is to redirect the user to an account page directly after the action that adds the message has been completed (after an email has been sent, for example). You have complete control over this from your views.