fromdjango.confimportsettingsfromdjango.contrib.messages.storageimportdefault_storagefromdjango.utils.deprecationimportMiddlewareMixinclassMessageMiddleware(MiddlewareMixin):""" Middleware that handles temporary messages. """defprocess_request(self,request):request._messages=default_storage(request)defprocess_response(self,request,response):""" Update the storage backend (i.e., save the messages). Raise ValueError if not all messages could be stored and DEBUG is True. """# A higher middleware layer may return a request which does not contain# messages storage, so make no assumption that it will be there.ifhasattr(request,"_messages"):unstored_messages=request._messages.update(response)ifunstored_messagesandsettings.DEBUG:raiseValueError("Not all temporary messages could be stored.")returnresponse