報錯:TypeError: BasicAuthentication() takes no arguments,我用的python3.9
时间: 2024-03-05 21:48:28 浏览: 13
这个错误通常是因为`BasicAuthentication`类的构造函数定义了参数,而在实例化时没有传递参数导致的。从`django-rest-framework` 3.13版本开始,`BasicAuthentication`类的构造函数不再接受任何参数,因此如果你的代码中使用了参数,就会报出这个错误。
解决方法是将`BasicAuthentication`实例化时的参数移除。如果你在自定义`BasicAuthentication`类时使用了参数,可以将其去除。例如,如果你的代码类似于这样:
```python
class MyBasicAuthentication(BasicAuthentication):
def __init__(self, realm):
self.realm = realm
super().__init__()
def authenticate(self, request):
# ...
```
你需要将`__init__`方法中的`realm`参数去除,改为如下形式:
```python
class MyBasicAuthentication(BasicAuthentication):
def __init__(self):
super().__init__()
def authenticate(self, request):
# ...
```
如果你在其他地方使用了`BasicAuthentication`的实例化参数,也需要将其去除。