I add a message like this in my view:
from django.contrib import messages
messages.success(request, 'contact-ok')
How can I check for the presence of a specific message? I tried like this
{% if messages %}
{% for message in messages %}
{% if message == 'contact-ok' %}
alert('ok');
{% endif %}
{% endfor %}
{% endif %}
but somehow message can be printed like a string but not compared like a string and alert doesn’t get called. Any help?
Each message in
messagesis a python object, not a string. You can see theMessageclass indjango.contrib.messages.storages.base. You’ll notice that eachMessagealso has amessageattribute, so instead of trying to compare object to string, use the message attribute of the object: