我正在使用 Knox 进行令牌身份验证。它在开发过程中运行良好,但现在我尝试在 PythonAnywhere 上进行部署,我得到标题中的错误:“TypeError:create() 采用 1 个位置参数,但给出了 2 个”。
这是完整的追溯:
2020-09-10 13:42:03,788: Internal Server Error: /api/auth/login
Traceback (most recent call last):
File "/home/<user>/.virtualenvs/<website>/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/<user>/.virtualenvs/<website>/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/<user>/.virtualenvs/<website>/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/<user>/.virtualenvs/<website>/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/<user>/.virtualenvs/<website>/lib/python3.6/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/home/<user>/.virtualenvs/<website>/lib/python3.6/site-packages/rest_framework/views.py", line 505, in dispatch
response = self.handle_exception(exc)
File "/home/<user>/.virtualenvs/<website>/lib/python3.6/site-packages/rest_framework/views.py", line 465, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/<user>/.virtualenvs/<website>/lib/python3.6/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
raise exc
File "/home/<user>/.virtualenvs/<website>/lib/python3.6/site-packages/rest_framework/views.py", line 502, in dispatch
response = handler(request, *args, **kwargs)
File "/home/<user>/<website>/registration/views.py", line 38, in post
"token": AuthToken.objects.create(user)[1]
File "/home/<user>/.virtualenvs/<website>/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
TypeError: create() takes 1 positional argument but 2 were given
我的registration.views(倒数第二行出现错误):
class RegisterDetail(generics.GenericAPIView):
serializer_class = RegisterSerializer
def post(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
user = serializer.save()
return Response({
"user": UserSerializer(user, context=self.get_serializer_context()).data,
"token": AuthToken.objects.create(user)[1]
})
class LoginDetail(generics.GenericAPIView):
serializer_class = LoginSerializer
def post(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
user = serializer.validated_data
return Response({
"user": UserSerializer(user, context=self.get_serializer_context()).data,
"token": AuthToken.objects.create(user)[1]
})