Middleware¶
MessageMiddleware
¶
- class djem.middleware.MessageMiddleware[source]¶
New in version 0.6.
Middleware that handles temporary messages, differentiating between those added as part of an AJAX request vs those that are not. Different storage backends are used for each: the default backend (configurable via
MESSAGE_STORAGE
) for standard requests and a custom memory-only backend for AJAX requests.Using a memory-only backend, which does not offer any persistence between requests, prevents simultaneous AJAX requests interfering with each other’s message stores, avoiding the documented caveat of Django’s out-of-the-box messages framework.
This is a drop-in replacement for Django’s own
MessageMiddleware
:# before MIDDLEWARE = [ ... 'django.contrib.messages.middleware.MessageMiddleware' ... ] # after MIDDLEWARE = [ ... 'djem.middleware.MessageMiddleware' ... ]
Important
MessageMiddleware
uses the HttpRequest
object’s is_ajax()
method to differentiate between AJAX and non-AJAX requests. An XMLHttpRequest
call must use the appropriate headers in order to be correctly detected.
See also
AjaxResponse
An extension of Django’s
JsonResponse
that, among other things, will automatically include any messages that are in the message store as part of the response.