What does TypeError, __init__() missing 1 required positional argument: 'get_response' mean in python?
Question:
I’m following the graphql python tutorial at https://www.howtographql.com/graphql-python/4-authentication/. It worked fine for the first 3 sections, but in the Authentication section I’ve run into this problem.
I am learning python, don’t know Django or graphql, so it’s a lot to digest all at once, but it was going ok until now. Also not sure what relevant bits to include here.
I followed all the instructions. When I go to my local project site at localhost:8000/graphql/
, I get
TypeError at /graphql/
__init__() missing 1 required positional argument: 'get_response'
Here is the relevant snippet of my settings.py:
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.contrib.auth.middleware.AuthenticationMiddleware',
]
GRAPHENE = {
'SCHEMA': 'hackernews.schema.schema',
'MIDDLEWARE': ['graphql_jwt.middleware.JSONWebTokenMiddleware', ],
}
AUTHENTICATION_BACKENDS = [
'graphql_jwt.backends.JSONWebTokenBackend',
'django.contrib.auth.backends.ModelBackend',
]
I also did import graphql_jwt in my main schema.py
Here is some kind of stack trace
Environment:
Request Method: GET
Request URL: http://localhost:8000/graphql/
Django Version: 2.1.4
Python Version: 3.7.4
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'graphene_django',
'links']
Installed 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.contrib.auth.middleware.AuthenticationMiddleware']
Traceback:
File "C:Userse79909projectspythongraphql-pythonvenvlibsite-packagesdjangocorehandlersexception.py" in inner
34. response = get_response(request)
File "C:Userse79909projectspythongraphql-pythonvenvlibsite-packagesdjangocorehandlersbase.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "C:Userse79909projectspythongraphql-pythonvenvlibsite-packagesdjangocorehandlersbase.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:Userse79909projectspythongraphql-pythonvenvlibsite-packagesdjangoviewsdecoratorscsrf.py" in wrapped_view
54. return view_func(*args, **kwargs)
File "C:Userse79909projectspythongraphql-pythonvenvlibsite-packagesdjangoviewsgenericbase.py" in view
62. self = cls(**initkwargs)
File "C:Userse79909projectspythongraphql-pythonvenvlibsite-packagesgraphene_djangoviews.py" in __init__
88. self.middleware = list(instantiate_middleware(middleware))
File "C:Userse79909projectspythongraphql-pythonvenvlibsite-packagesgraphene_djangoviews.py" in instantiate_middleware
48. yield middleware()
Exception Type: TypeError at /graphql/
Exception Value: __init__() missing 1 required positional argument: 'get_response'
Answers:
Ok, I just found it.
GRAPHENE = {
'SCHEMA': 'hackernews.schema.schema',
'MIDDLEWARES': ['graphql_jwt.middleware.JSONWebTokenMiddleware'],
}
Notice the S
. It needs to be 'MIDDLEWARES'
, not 'MIDDLEWARE'
.
Found the solution on this GitHub issue
Also, according to this comment on the same issue, you should add 'graphql_jwt.middleware.JSONWebTokenMiddleware'
to the MIDDLEWARE
list (the one with all the Django middleware).
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'graphql_jwt.middleware.JSONWebTokenMiddleware', ### <---Add this line
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
upgrade django-graphql-jwt to 0.3.4 (or higher) from the tutorial’s 0.1.5
Faced this issue when we moved from Django 2.2 to Django 4.0.5 version.
Updated the DRF version from 3.12.0 to 3.14.0 to fix this issue.
pip install djangorestframework==3.14.0
I’m following the graphql python tutorial at https://www.howtographql.com/graphql-python/4-authentication/. It worked fine for the first 3 sections, but in the Authentication section I’ve run into this problem.
I am learning python, don’t know Django or graphql, so it’s a lot to digest all at once, but it was going ok until now. Also not sure what relevant bits to include here.
I followed all the instructions. When I go to my local project site at localhost:8000/graphql/
, I get
TypeError at /graphql/
__init__() missing 1 required positional argument: 'get_response'
Here is the relevant snippet of my settings.py:
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.contrib.auth.middleware.AuthenticationMiddleware',
]
GRAPHENE = {
'SCHEMA': 'hackernews.schema.schema',
'MIDDLEWARE': ['graphql_jwt.middleware.JSONWebTokenMiddleware', ],
}
AUTHENTICATION_BACKENDS = [
'graphql_jwt.backends.JSONWebTokenBackend',
'django.contrib.auth.backends.ModelBackend',
]
I also did import graphql_jwt in my main schema.py
Here is some kind of stack trace
Environment:
Request Method: GET
Request URL: http://localhost:8000/graphql/
Django Version: 2.1.4
Python Version: 3.7.4
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'graphene_django',
'links']
Installed 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.contrib.auth.middleware.AuthenticationMiddleware']
Traceback:
File "C:Userse79909projectspythongraphql-pythonvenvlibsite-packagesdjangocorehandlersexception.py" in inner
34. response = get_response(request)
File "C:Userse79909projectspythongraphql-pythonvenvlibsite-packagesdjangocorehandlersbase.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "C:Userse79909projectspythongraphql-pythonvenvlibsite-packagesdjangocorehandlersbase.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:Userse79909projectspythongraphql-pythonvenvlibsite-packagesdjangoviewsdecoratorscsrf.py" in wrapped_view
54. return view_func(*args, **kwargs)
File "C:Userse79909projectspythongraphql-pythonvenvlibsite-packagesdjangoviewsgenericbase.py" in view
62. self = cls(**initkwargs)
File "C:Userse79909projectspythongraphql-pythonvenvlibsite-packagesgraphene_djangoviews.py" in __init__
88. self.middleware = list(instantiate_middleware(middleware))
File "C:Userse79909projectspythongraphql-pythonvenvlibsite-packagesgraphene_djangoviews.py" in instantiate_middleware
48. yield middleware()
Exception Type: TypeError at /graphql/
Exception Value: __init__() missing 1 required positional argument: 'get_response'
Ok, I just found it.
GRAPHENE = {
'SCHEMA': 'hackernews.schema.schema',
'MIDDLEWARES': ['graphql_jwt.middleware.JSONWebTokenMiddleware'],
}
Notice the S
. It needs to be 'MIDDLEWARES'
, not 'MIDDLEWARE'
.
Found the solution on this GitHub issue
Also, according to this comment on the same issue, you should add 'graphql_jwt.middleware.JSONWebTokenMiddleware'
to the MIDDLEWARE
list (the one with all the Django middleware).
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'graphql_jwt.middleware.JSONWebTokenMiddleware', ### <---Add this line
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
upgrade django-graphql-jwt to 0.3.4 (or higher) from the tutorial’s 0.1.5
Faced this issue when we moved from Django 2.2 to Django 4.0.5 version.
Updated the DRF version from 3.12.0 to 3.14.0 to fix this issue.
pip install djangorestframework==3.14.0