Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does TypeError, __init__() missing 1 required positional argument: 'get_response' mean in python?

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:\Users\e79909\projects\python\graphql-python\venv\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Users\e79909\projects\python\graphql-python\venv\lib\site-packages\django\core\handlers\base.py" in _get_response
  126.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\e79909\projects\python\graphql-python\venv\lib\site-packages\django\core\handlers\base.py" in _get_response
  124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\e79909\projects\python\graphql-python\venv\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
  54.         return view_func(*args, **kwargs)

File "C:\Users\e79909\projects\python\graphql-python\venv\lib\site-packages\django\views\generic\base.py" in view
  62.             self = cls(**initkwargs)

File "C:\Users\e79909\projects\python\graphql-python\venv\lib\site-packages\graphene_django\views.py" in __init__
  88.             self.middleware = list(instantiate_middleware(middleware))

File "C:\Users\e79909\projects\python\graphql-python\venv\lib\site-packages\graphene_django\views.py" in instantiate_middleware
  48.             yield middleware()

Exception Type: TypeError at /graphql/
Exception Value: __init__() missing 1 required positional argument: 'get_response'
like image 381
user26270 Avatar asked Jun 08 '20 21:06

user26270


1 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',
]
like image 120
tbrlpld Avatar answered Sep 30 '22 13:09

tbrlpld