'Populate middleware lists from settings.MIDDLEWARE. This is copied
from Django. This uses settings.MIDDLEWARE setting with the old
business logic. The middleware architecture is not compatible
with our asynchronous handlers. The problem occurs when we return
None from our handler. The Django middlewares throw exception
because they can\'t handler None, so we can either upgrade the Django
middlewares or just override this method to use the new setting with
the old logic. The added advantage is that due to this our event
system code doesn\'t change.'

def load_middleware(self):

    self._request_middleware = []
    self._view_middleware = []
    self._template_response_middleware = []
    self._response_middleware = []
    self._exception_middleware = []
    handler = convert_exception_to_response(self._legacy_get_response)
    for middleware_path in settings.MIDDLEWARE:
        mw_class = import_string(middleware_path)
        try:
            mw_instance = mw_class()
        except MiddlewareNotUsed as exc:
            if settings.DEBUG:
                if six.text_type(exc):
                    base.logger.debug('MiddlewareNotUsed(%r):    %s', middleware_path, exc)
                else:
                    base.logger.debug('MiddlewareNotUsed:    %r', middleware_path)
            continue
        if hasattr(mw_instance, 'process_request'):
            self._request_middleware.append(mw_instance.process_request)
        if hasattr(mw_instance, 'process_view'):
            self._view_middleware.append(mw_instance.process_view)
        if hasattr(mw_instance, 'process_template_response'):
            self._template_response_middleware.insert(0, mw_instance.process_template_response)
        if hasattr(mw_instance, 'process_response'):
            self._response_middleware.insert(0, mw_instance.process_response)
        if hasattr(mw_instance, 'process_exception'):
            self._exception_middleware.insert(0, mw_instance.process_exception)
    self._middleware_chain = handler
