How to curb the Django messages?

Hi all!
Have a long-standing problem with the results in the django template messages. I personally need them, in one case, just one page. So in the view I set a message and redirect:
if not available_cars:
 carstoshow = None
 messages.error(request, 'Your date range busy for this object.')
 return redirect('car_detail', pk=car_used_id)

Show a message in the template the standard way:
{% if messages %}
<div class="alert alert-warning">
<div class="container text-center">
 {% for message in messages %}
 <li{% if message.tags %} class="{{ message.tags }}" {% endif>{{ message }}
 {% endfor %}
{% endif %}

Everything worked perfectly. But after trying several times to login/logout on that page I get a bunch of messages associated with django-allauth to authentication(the same as in the admin):

I tried to set its message as messages.error and so on. To weed out the junk in the template:
{% if messages.tipmaster %}
{% for message in messages.tipmaster %}

nothing comes out of either a whole pile or nothing.

Question: how can I allow only my posts.

Thanks, I will be grateful for any help
July 9th 19 at 10:47
1 answer
July 9th 19 at 10:49
Let's see how does the framework messages.
def messages(request):
 return {
 'messages': get_messages(request),
def get_messages(request):
 if hasattr(request, '_messages'):
 return request._messages
 return []

So messages get to the page. Ie they are taken from the request._messages. How did they get there?
class MessageMiddleware(MiddlewareMixin):
 def process_request(self, request):
 request._messages = default_storage(request)
def default_storage(request):
 return import_string(settings.MESSAGE_STORAGE)(request)

So in request._messages we have is FallbackStorage(request)

Let's move on to our code
messages.error(request, 'Your date range busy for this object.')

What's going on here? View:
def error(request, message, extra_tags=", fail_silently=False):
 add_message(request, constants.ERROR, message, extra_tags=extra_tags,
def add_message(request, level, message, extra_tags=", fail_silently=False):
 if not isinstance(request, HttpRequest):
 raise TypeError("add_message() argument must be an HttpRequest object "
 "not '%s'." % request.__class__.__name__)
 if hasattr(request, '_messages'):
 return request._messages.add(level, message, extra_tags) # *** THIS ***
 if not fail_silently:
 raise MessageFailure('You cannot add messages without installing '

Yeah, you call the add method familiar to us FallbackStorage object.
It is passed as parameters:
- level = constants.ERROR = 40
- message = 'Your date range busy for this object.'
- extra_tags = "

Look at this method
def add(self, level, message, extra_tags="):
 if not message:
 level = int(level)
 if level < self.level:
 # Add the message.
 self.added_new = True
 message = Message(level, message, extra_tags=extra_tags)
def _get_level(self):
 if not hasattr(self, '_level'):
 self._level = getattr(settings, 'MESSAGE_LEVEL', constants.INFO)
 return self._level

From this code we can conclude that the message arrives in the message queue if its level is more or equal to that specified in settings.MESSAGE_LEVEL (default = contants.INFO = 20)

Thus were added to only our messages should:
1) install

2) In view to write
if not available_cars:
 carstoshow = None
 messages.add_message(request, settings.MY_SUPER_ERROR, 'Your date range busy for this object.')
 return redirect('car_detail', pk=car_used_id)
the best answer I've seen on the toaster! thank you, it worked - allie.Homenick commented on July 9th 19 at 10:52

Find more questions by tags Django