Creating CustomMiddleware

I created a custom mysite/mymiddlewar.py file as follows…

class DoseControllerMiddleware(object):

def process_request(self, request):
    """
    get ready to call the Dose Controller
    """
    print("DoseControllerModdleware.process_request Hooray")
    return None

This is in a file called mymiddleware located adjacent to mysite.settings.py

My settings looks like this…

MIDDLEWARE = [
‘django.middleware.security.SecurityMiddleware’,
‘django.contrib.sessions.middleware.SessionMiddleware’,
‘mysite.mymiddleware.DoseControllerMiddleware’,
‘django.middleware.common.CommonMiddleware’,
‘django.middleware.csrf.CsrfViewMiddleware’,
‘django.contrib.auth.middleware.AuthenticationMiddleware’,
‘django.contrib.messages.middleware.MessageMiddleware’,
‘django.middleware.clickjacking.XFrameOptionsMiddleware’,
]

When I runserver I get…

ModuleNotFoundError: No module named ‘mysite.mymiddleware’

I have tried it with a middleware folder with init with the mymiddleware.py in that folder and changed the path in MIDDLEWARE settings to match but ModuleNotFoundError.

This might be a typo in your post rather than the filename, but are you sure “mysite/mymiddlewar.py” is correct? It must be the same as the full module path listed in the MIDDLEWARE setting, except with dot-notation instead of filepath-notation.

Other than that, is this the full code for the middleware class? The documentation on middleware specifies different hooks than process_request for processing requests. However, this shouldn’t have anything to do with the module not being found.