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 %}
</li{%></div>
</div>
{% 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):
6f15480572a74ba0882ca412e77eedad.png6ab10c5c1e1445d69e2a02bc37184dc2.png

I tried to set its message as messages.error messages.info 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
Solution
Let's see how does the framework messages.

https://github.com/django/django/blob/master/djang...
def messages(request):
 return {
 'messages': get_messages(request),
 'DEFAULT_MESSAGE_LEVELS': DEFAULT_LEVELS,
}
...
def get_messages(request):
 if hasattr(request, '_messages'):
 return request._messages
else:
 return []

So messages get to the page. Ie they are taken from the request._messages. How did they get there?

https://github.com/django/django/blob/master/djang...
class MessageMiddleware(MiddlewareMixin):
 def process_request(self, request):
 request._messages = default_storage(request)
...
def default_storage(request):
 return import_string(settings.MESSAGE_STORAGE)(request)
...
MESSAGE_STORAGE = 'django.contrib.messages.storage.fallback.FallbackStorage'

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:
https://github.com/django/django/blob/master/djang...
def error(request, message, extra_tags=", fail_silently=False):
 add_message(request, constants.ERROR, message, extra_tags=extra_tags,
fail_silently=fail_silently)
...
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 '
 'django.contrib.messages.middleware.MessageMiddleware')

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
https://github.com/django/django/blob/master/djang...
def add(self, level, message, extra_tags="):
 if not message:
return
 level = int(level)
 if level < self.level:
return
 # Add the message.
 self.added_new = True
 message = Message(level, message, extra_tags=extra_tags)
self._queued_messages.append(message)
...
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) settings.py install
# settings.py
...
MY_SUPER_ERROR = 80
MESSAGE_LEVEL = MY_SUPER_ERROR
...

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