permission() for permission in self.permission_classes -> TypeError: 'str' object is not callable
permission() for permission in self.permission_classes -> TypeError: 'str' object is not callable
有人知道我为什么会收到此错误吗?我将默认权限设置为 IsAuthenticated。只有register有AllowAny,允许用户注册。
Not Found: /
[21/Feb/2022 21:43:06] "GET / HTTP/1.1" 404 2278
Not Found: /account
[21/Feb/2022 21:43:11] "GET /account HTTP/1.1" 404 2317
Internal Server Error: /account/register
Traceback (most recent call last):
\venv\lib\site-packages\rest_framework\views.py", line 278, in <listcomp>
return [permission() for permission in self.permission_classes]
TypeError: 'str' object is not callable
[21/Feb/2022 21:43:14] "GET /account/register HTTP/1.1" 500 106055
我的views.py
class:
@api_view(['POST'])
@permission_classes(['AllowAny'])
def registration_view(request):
serializer = RegistrationSerializer(data=request.data)
data = {}
if serializer.is_valid():
account = serializer.save()
data['response'] = "successfully registered a new user."
data['email'] = account.email
data['username'] = account.username
else:
data = serializer.errors
return Response(data)
我在 settings.py 文件中使用 SessionAuthentication 和 IsAuthenticated 作为默认身份验证和权限。
CustomUser
模型只是继承自 AbstractUser
。没有添加。
您不能将权限 类 作为字符串传递,而是作为对 类 的引用传递,因此:
from rest_framework.permissions import <strong>AllowAny</strong>
@api_view(['POST'])
@permission_classes([<strong>AllowAny</strong>])
def registration_view(request):
# …
但您无需指定 AllowAny
,因为它允许任何请求。您可以使用以下方式实现视图:
@api_view(['POST'])
def registration_view(request):
# …
有人知道我为什么会收到此错误吗?我将默认权限设置为 IsAuthenticated。只有register有AllowAny,允许用户注册。
Not Found: /
[21/Feb/2022 21:43:06] "GET / HTTP/1.1" 404 2278
Not Found: /account
[21/Feb/2022 21:43:11] "GET /account HTTP/1.1" 404 2317
Internal Server Error: /account/register
Traceback (most recent call last):
\venv\lib\site-packages\rest_framework\views.py", line 278, in <listcomp>
return [permission() for permission in self.permission_classes]
TypeError: 'str' object is not callable
[21/Feb/2022 21:43:14] "GET /account/register HTTP/1.1" 500 106055
我的views.py
class:
@api_view(['POST'])
@permission_classes(['AllowAny'])
def registration_view(request):
serializer = RegistrationSerializer(data=request.data)
data = {}
if serializer.is_valid():
account = serializer.save()
data['response'] = "successfully registered a new user."
data['email'] = account.email
data['username'] = account.username
else:
data = serializer.errors
return Response(data)
我在 settings.py 文件中使用 SessionAuthentication 和 IsAuthenticated 作为默认身份验证和权限。
CustomUser
模型只是继承自 AbstractUser
。没有添加。
您不能将权限 类 作为字符串传递,而是作为对 类 的引用传递,因此:
from rest_framework.permissions import <strong>AllowAny</strong>
@api_view(['POST'])
@permission_classes([<strong>AllowAny</strong>])
def registration_view(request):
# …
但您无需指定 AllowAny
,因为它允许任何请求。您可以使用以下方式实现视图:
@api_view(['POST'])
def registration_view(request):
# …