Very rarely, a Django message is rendered in an HTML
response. Then, the user GET
s another page, and the Django message is rendered again. Sometimes, the user will GET
another page thereafter, and the same message will display once again.
This happens very rarely, but when it does, most users see the same behavior for a few moments. (It happened once in a group cross-browser test, and everyone in the room saw the same behavior on each of their computers for about five minutes before going away.)
This behavior has occurred with many different views; and besides, each view adds them in the same way (see below).
I've been unable to reliably reproduce the error, either in our deployed environment (running wsgi.py
) or when running the project on a local environment (running manage.py
). (I will note that I've never seen this problem locally.)
Does anyone have any idea why this might be happening?
Example of a success message being added.
messages.success( request, "Changes to {form} {request} were successfully saved."\ .format(form=self.form.display_name, request=serv_request_id_as_url(self.serv_request)) )
This is the template code which renders the message:
<div id="messages"> {% for message in messages %} <div class="alert alert-{{ message.tags }}"> <a class="close" data-dismiss="alert" href="#" title="Close"><span class="accessibility">Close this message.</span>×</a> <strong>{{ message.message|safe }}</strong> </div> {% endfor %} </div>
Relevant settings:
MIDDLEWARE_CLASSES = ( 'django.middleware.gzip.GZipMiddleware', 'django.middleware.common.CommonMiddleware', 'outage.middleware.OutageMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.transaction.TransactionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'utdirect.middleware.HttpHeaderMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', ... ) MESSAGE_STORAGE = 'django.contrib.messages.storage.session.SessionStorage' DATABASES = {'default': {'ENGINE': 'django.db.backends.oracle', ...}}
You need to iterate through the messages for your current set of messages as retrieved from the 'get_messages' method. You can use this code anywhere you want to clear the messages before setting new ones or where you want to simply clear all messages.
For this, Django provides full support for cookie- and session-based messaging, for both anonymous and authenticated users. The messages framework allows you to temporarily store messages in one request and retrieve them for display in a subsequent request (usually the next one).
Add a Django success messagecontrib at the top of the file then go to the view function where you wish to add the message(s). In this case, it's a contact view that needs a Django success message and a Django error message. Add a success message just before the return redirect ("main:homepage") stating "Message sent."
It seems this a noted problem caused by caching in Django. Here is the comment made by one Django core developer:
"I don't think Django can handle this problem automatically and correctly. It is a very common pattern to loop over messages in the base template of a website. If we disable caching simply because messages might be displayed on a given page, we're just killing the cache for most websites."
The full ticket is here: https://code.djangoproject.com/ticket/13894
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With