gpt4 book ai didi

python - 什么类型错误,__init__() 缺少 1 个必需的位置参数 : 'get_response' mean in python?

转载 作者:行者123 更新时间:2023-12-03 16:11:11 24 4
gpt4 key购买 nike

我正在关注 https://www.howtographql.com/graphql-python/4-authentication/ 上的 graphql python 教程.它在前 3 个部分运行良好,但在身份验证部分我遇到了这个问题。

我正在学习 python,不知道 Django 或 graphql,所以一次消化很多,但到目前为止一切正常。也不确定要在此处包含哪些相关位。

我遵循了所有说明。当我在 localhost:8000/graphql/ 访问我的本地项目站点时,我得到
TypeError at /graphql/__init__() missing 1 required positional argument: 'get_response'
这是我的 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',
]

我还在我的主 schema.py 中导入了 graphql_jwt

这是某种堆栈跟踪
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'

最佳答案

好的,我刚找到。

GRAPHENE = {
'SCHEMA': 'hackernews.schema.schema',
'MIDDLEWARES': ['graphql_jwt.middleware.JSONWebTokenMiddleware'],
}

请注意 S .它需要是 'MIDDLEWARES' ,不是 'MIDDLEWARE' .

在此找到解决方案 GitHub issue

此外,根据 this comment在同一问题上,您应该添加 'graphql_jwt.middleware.JSONWebTokenMiddleware'MIDDLEWARE列表(包含所有 Django 中间件的列表)。

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',
]

关于python - 什么类型错误,__init__() 缺少 1 个必需的位置参数 : 'get_response' mean in python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62271614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com