Osman Hamashool
Osman Hamashool

Reputation: 324

TypeError: __init__() missing 1 required positional argument: 'get_response'

when i run my server python3 manage.py runserver the browser returns A server error occurred. Please contact the administrator. then i get this error

`Traceback (most recent call last):
  File "/usr/lib/python3.5/wsgiref/handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "/usr/local/lib/python3.5/dist-packages/django/contrib/staticfiles/handlers.py", line 63, in __call__
    return self.application(environ, start_response)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/wsgi.py", line 170, in __call__
    self.load_middleware()
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 52, in load_middleware
    mw_instance = mw_class()
TypeError: __init__() missing 1 required positional argument: 'get_response'

my settings file is like that

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'accounts',
]

MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'web.middleware.LoginRequiredMiddleware',
]

and in may project i created middleware.py and also it looks like this

from django.conf import settings

class LoginRequiredMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

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

so anay idea?!

Upvotes: 5

Views: 8858

Answers (3)

Osman Hamashool
Osman Hamashool

Reputation: 324

Now i solved my error. I update my django version from django 1.8 to django 1.11,

sudo pip install django==1.11

and changed MIDDLEWARE_CLASS TO MIDDLEWARE in settings.py

and then migrate database

python manage.py migrate

and it's worked well

Upvotes: 6

Alasdair
Alasdair

Reputation: 309089

Your middleware is a new style middleware, so you should be using the MIDDLEWARE instead of MIDDLEWARE_CLASSES in your settings.

Upvotes: 7

Primusa
Primusa

Reputation: 13498

When you instantiate your class you need to input get_response as a parameter because your class automatically runs its init() method. So:

newinstance = LoginRequiredMiddleware(response)

If you leave the parentheses blank init is missing the parameter that you set it to take.

Upvotes: 0

Related Questions