django-async-messages icon indicating copy to clipboard operation
django-async-messages copied to clipboard

Get error related to get_wsgi_application() and middleware handler

Open calcutec opened this issue 6 years ago • 2 comments

mw_instance = middleware(handler)

TypeError: object() takes no parameters

This is the complete trace:

System check identified no issues (0 silenced). May 24, 2019 - 5:18:33 PM Django version 2.1.7, using settings 'djangodigital.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK. Unhandled exception in thread started by <_pydev_bundle.pydev_monkey.NewThreadStartupWithTrace object at 0x0689EF10> Traceback (most recent call last): File "C:\Program Files\JetBrains\PyCharm 2017.2.3\helpers\pydev_pydev_bundle\pydev_monkey.py", line 589, in call return self.original_func(*self.args, **self.kwargs) File "C:\Users\burton\Projects\djangodigital\dpmvenv\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper fn(*args, **kwargs) File "C:\Users\burton\Projects\djangodigital\dpmvenv\lib\site-packages\django\core\management\commands\runserver.py", line 137, in inner_run handler = self.get_handler(*args, **options) File "C:\Users\burton\Projects\djangodigital\dpmvenv\lib\site-packages\django\contrib\staticfiles\management\commands\runserver.py", line 27, in get_handler handler = super().get_handler(*args, **options) File "C:\Users\burton\Projects\djangodigital\dpmvenv\lib\site-packages\django\core\management\commands\runserver.py", line 64, in get_handler return get_internal_wsgi_application() File "C:\Users\burton\Projects\djangodigital\dpmvenv\lib\site-packages\django\core\servers\basehttp.py", line 45, in get_internal_wsgi_application return import_string(app_path) File "C:\Users\burton\Projects\djangodigital\dpmvenv\lib\site-packages\django\utils\module_loading.py", line 17, in import_string module = import_module(module_path) File "C:\Python36_32\lib\importlib_init.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 994, in _gcd_import File "", line 971, in _find_and_load File "", line 955, in _find_and_load_unlocked File "", line 665, in _load_unlocked File "", line 678, in exec_module File "", line 219, in _call_with_frames_removed File "C:\Users\burton\Projects\djangodigital\djangodigital\wsgi.py", line 16, in application = get_wsgi_application() File "C:\Users\burton\Projects\djangodigital\dpmvenv\lib\site-packages\django\core\wsgi.py", line 13, in get_wsgi_application return WSGIHandler() File "C:\Users\burton\Projects\djangodigital\dpmvenv\lib\site-packages\django\core\handlers\wsgi.py", line 136, in init self.load_middleware() File "C:\Users\burton\Projects\djangodigital\dpmvenv\lib\site-packages\django\core\handlers\base.py", line 36, in load_middleware mw_instance = middleware(handler) TypeError: object() takes no parameters

calcutec avatar May 24 '19 21:05 calcutec

Looks like a few things have changed in recent Django versions that break this (handy and nice) script.

Fixed by adding the MiddlewareMixin to middleware.py, as described here:

https://stackoverflow.com/questions/42232606/django-exception-middleware-typeerror-object-takes-no-parameters

Also, got error that request.user.is_authenticated() is not callable. Changed this to "request.user.is_authenticated" (no function parentheses) and the script below works great.

from django.http import HttpResponse from django.utils.deprecation import MiddlewareMixin

from django.contrib import messages

from async_messages import get_messages

class AsyncMiddleware(MiddlewareMixin):

def process_response(self, request, response):
    """
    Check for messages for this user and, if it exists,
    call the messages API with it
    """

    if hasattr(request, "session") and hasattr(request, "user") and request.user.is_authenticated:
        msgs = get_messages(request.user)
        if msgs:
            for msg, level in msgs:
                messages.add_message(request, level, msg)
    return response

calcutec avatar May 24 '19 21:05 calcutec

Great, works for me!

Klexus1 avatar Apr 13 '22 09:04 Klexus1