【问题标题】:MiddlewareMixin missing required argument: 'get_response' djangoMiddlewareMixin 缺少必需的参数:'get_response' django
【发布时间】:2022-01-05 07:25:56
【问题描述】:

有人对我为什么会收到此错误有任何想法吗?我的程序以前可以运行,但我不知道我更改了什么导致它崩溃。

我的主网站可以正常工作,但每当我向http://10.0.0.233:8000/watcher/form/list 发出此获取请求时,我都会收到以下错误。我搜索了整个项目,没有发现 MiddlewareMixin 在任何地方使用。

urls.py:

from django.urls import path
from . import views

urlpatterns = [
    path('form/list',views.get_all_form_items),
]

Views.py

from django.shortcuts import render
from rest_framework.decorators import api_view
from ast import literal_eval
from rest_framework.response import Response
import json

import sys
sys.path.append("...Utilities") 

from Utilities import string_math

from . models import form_item_db
from . serializers import form_item_db_serializer
@api_view(['GET'])
def get_all_form_items(request):
    snippets = form_item_db.objects.all()
    serializer = form_item_db_serializer(snippets, many=True)
        
    return Response(serializer.data)

错误:

Django version 4.0, using settings 'backend.settings'
Starting development server at http://10.0.0.233:8000/
Quit the server with CTRL-BREAK.
[05/Jan/2022 02:11:55] "GET /watcher HTTP/1.1" 200 644
[05/Jan/2022 02:11:55] "GET /static/js/main.1924b030.js HTTP/1.1" 304 0
[05/Jan/2022 02:11:55] "GET /static/css/main.31d6cfe0.css HTTP/1.1" 304 0
Internal Server Error: /watcher/form/list
Traceback (most recent call last):
  File "C:\Users\bestg\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\bestg\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\bestg\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Users\bestg\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\generic\base.py", line 69, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\bestg\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\views.py", line 505, in dispatch
    response = self.handle_exception(exc)
  File "C:\Users\bestg\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\views.py", line 465, in handle_exception
    self.raise_uncaught_exception(exc)
  File "C:\Users\bestg\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\views.py", line 476, in raise_uncaught_exception
    raise exc
  File "C:\Users\bestg\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\views.py", line 493, in dispatch
    self.initial(request, *args, **kwargs)
  File "C:\Users\bestg\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\views.py", line 410, in initial
    self.perform_authentication(request)
  File "C:\Users\bestg\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\views.py", line 324, in perform_authentication
    request.user
  File "C:\Users\bestg\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\request.py", line 220, in user
    self._authenticate()
  File "C:\Users\bestg\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\request.py", line 373, in _authenticate
    user_auth_tuple = authenticator.authenticate(self)
  File "C:\Users\bestg\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\authentication.py", line 126, in authenticate
    self.enforce_csrf(request)
  File "C:\Users\bestg\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\authentication.py", line 135, in enforce_csrf
    check = CSRFCheck()
TypeError: MiddlewareMixin.__init__() missing 1 required positional argument: 'get_response'

【问题讨论】:

  • 你改过django或rest框架的版本吗?另外,您使用的是哪些版本的软件包?
  • @markwalker_ 当我创建我相信的项目时,我最初使用的是 django ~3.0。不过,我升级到 4.0 并且几天没有任何问题,直到发生这种情况。我应该检查哪些包裹?
  • Ok DRF 支持 django 4,所以这里可能有其他问题。您的中间件类中的某些内容不适用于您正在使用的 django 版本。您需要找出该应用程序是什么,然后寻找更新或其他解决方案。关于这种新型中间件的更多信息在这里; stackoverflow.com/questions/56496132/…

标签: python python-3.x django


【解决方案1】:

自 10.2020 以来,github 上的 DRF 包中有一个修复:https://github.com/encode/django-rest-framework/commit/7921e9af434f2ccfde6962cf8a1b76331cc77722#diff-25717930a68aebbdb51ee5f4060fb1e756d65ee4e8d96faf8ad614ceced0db05

rest_framework/authentication.py 第 139 行左右

check = CSRFCheck()

改为

def dummy_get_response(request):  # pragma: no cover
    return None

check = CSRFCheck(dummy_get_response)

【讨论】:

  • 感激不尽,非常感谢!! pip install -U djangorestframework 解决了我的问题!我真的在考虑重新设置我的 django 项目
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-20
  • 2021-11-05
  • 2018-10-28
  • 2018-12-01
  • 2016-06-29
  • 2018-06-20
相关资源
最近更新 更多