简体   繁体   中英

Django Middleware Error

I'm trying to write a custom django middleware package by referring an example here :

from django.conf import settings

class StackOverflowMiddleware(object):
    def process_exception(self, request, exception):
        if settings.DEBUG:
            print exception.__class__.__name__
            print exception.message
        return None

and this is present at location venv/lib/python2.7/site-packages/django_error_assist/middleware.py

I try to invoke/include this middleware in my django settings as follows:

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django_error_assist.middleware.StackOverflowMiddleware', ]

But I get a traceback which I've been struggling to get rid of which follows like this :

File "/Users/Shyam/PycharmProjects/untitled/untitled/wsgi.py", line 16, in <module>
    application = get_wsgi_application()
  File "/Users/Shyam/PycharmProjects/untitled/venv/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
    return WSGIHandler()
  File "/Users/Shyam/PycharmProjects/untitled/venv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 151, in __init__
    self.load_middleware()
  File "/Users/Shyam/PycharmProjects/untitled/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 82, in load_middleware
    mw_instance = middleware(handler)
TypeError: object() takes no parameters

Where am I going wrong ? Any leads/help will be appreciated. I'm trying to make this middleware a pip package.

I also did my homework of looking into this , this and of course the official django link but couldn't get much help from these as in where am I going wrong.

I suggest you to change all classes that took object to MiddlewareMixin .

class StackOverflowMiddleware(object):
    ....

to:

from django.utils.deprecation import MiddlewareMixin

class StackOverflowMiddleware(MiddlewareMixin):
    ....

In Django from version 1.10 Middleware must accept get_response agrument, see docs . Change your class to this:

class StackOverflowMiddleware(object):
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        return response

    def process_exception(self, request, exception):
        if settings.DEBUG:
            print exception.__class__.__name__
            print exception.message
        return None

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM