Django登录方法给出递归错误

Django登录方法给出递归错误,django,authentication,django-rest-framework,Django,Authentication,Django Rest Framework,我尝试使用login(request,user)创建一个自定义身份验证视图,在会话中保存用户,但它给了我 调用Python对象时超出了最大递归深度 为了避免混淆方法,我尝试从django.contrib.auth导入登录名为django_login的登录名为,但仍然不起作用 没有登录方法,身份验证就可以正常工作,但它不会在会话中保存用户,因此没有任何用处 以下是完整的views.py文件: from django.utils.translation import ugettext as _ fr

我尝试使用
login(request,user)
创建一个自定义身份验证视图,在会话中保存用户,但它给了我

调用Python对象时超出了最大递归深度

为了避免混淆方法,我尝试从django.contrib.auth导入登录名为django_login的登录名为
,但仍然不起作用

没有登录方法,身份验证就可以正常工作,但它不会在会话中保存用户,因此没有任何用处

以下是完整的
views.py
文件:

from django.utils.translation import ugettext as _
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework.views import APIView
from rest_framework.response import Response
from rest.models import User
from .serializers import UserSerializer
from django.contrib.auth import get_user_model
from django.contrib.auth import authenticate as django_authenticate
from django.contrib.auth import login as django_login
from django.contrib.auth.hashers import check_password
import json

class UserCreateAPIView(generics.CreateAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    permission_classes = (AllowAny,)

class Authentication(authentication.BaseAuthentication):

    def authenticate(self, request):
        email = request.POST.get('email', None)
        password = request.POST.get('password', None)
        if not email or not password:
            raise exceptions.AuthenticationFailed(_('No credentials provided.'))

        credentials = {
            get_user_model().USERNAME_FIELD: email,
            'password': password
        }

        user = django_authenticate(**credentials)

        if user is None:
            raise exceptions.AuthenticationFailed(_('Invalid username/password.'))

        if not user.is_active:
            raise exceptions.AuthenticationFailed(_('User inactive or deleted.'))

        django_login(request, user)
        return (user, None)  # authentication successful

class LoginView(APIView):
    authentication_classes = (SessionAuthentication, Authentication)
    permission_classes = (IsAuthenticated,)

    def post(self, request, format=None):
        content = {
            'user': str(request.user),
            'auth': str(request.auth),
        }
        return Response(content)

def CheckLoginView(requst):
    current_user = requst.user
    return current_user
以下是回溯:

            response = get_response(request) …
▶ Local vars
/var/www/cnmb10a/venv/lib/python3.7/site-packages/django/core/handlers/base.py in _get_response
                response = self.process_exception_by_middleware(e, request) …
▶ Local vars
/var/www/cnmb10a/venv/lib/python3.7/site-packages/django/core/handlers/base.py in _get_response
                response = wrapped_callback(request, *callback_args, **callback_kwargs) …
▶ Local vars
/var/www/cnmb10a/venv/lib/python3.7/site-packages/django/views/decorators/csrf.py in wrapped_view
        return view_func(*args, **kwargs) …
▶ Local vars
/var/www/cnmb10a/venv/lib/python3.7/site-packages/django/views/generic/base.py in view
            return self.dispatch(request, *args, **kwargs) …
▶ Local vars
/var/www/cnmb10a/venv/lib/python3.7/site-packages/rest_framework/views.py in dispatch
            response = self.handle_exception(exc) …
▶ Local vars
/var/www/cnmb10a/venv/lib/python3.7/site-packages/rest_framework/views.py in handle_exception
            self.raise_uncaught_exception(exc) …
▶ Local vars
/var/www/cnmb10a/venv/lib/python3.7/site-packages/rest_framework/views.py in raise_uncaught_exception
        raise exc …
▶ Local vars
/var/www/cnmb10a/venv/lib/python3.7/site-packages/rest_framework/views.py in dispatch
            self.initial(request, *args, **kwargs) …
▶ Local vars
/var/www/cnmb10a/venv/lib/python3.7/site-packages/rest_framework/views.py in initial
        self.perform_authentication(request) …
▶ Local vars
/var/www/cnmb10a/venv/lib/python3.7/site-packages/rest_framework/views.py in perform_authentication
        request.user …
▶ Local vars
/var/www/cnmb10a/venv/lib/python3.7/site-packages/rest_framework/request.py in user
                self._authenticate() …
▶ Local vars
/var/www/cnmb10a/venv/lib/python3.7/site-packages/rest_framework/request.py in _authenticate
                user_auth_tuple = authenticator.authenticate(self) …
▶ Local vars
/var/www/cnmb10a/cnmb10a/rest/auth_api/views.py in authenticate
        django_login(request, user) …
▶ Local vars
/var/www/cnmb10a/venv/lib/python3.7/site-packages/django/contrib/auth/__init__.py in login
    if hasattr(request, 'user'): …
▶ Local vars
/var/www/cnmb10a/venv/lib/python3.7/site-packages/rest_framework/request.py in user
                self._authenticate() …
▶ Local vars
/var/www/cnmb10a/venv/lib/python3.7/site-packages/rest_framework/request.py in _authenticate
                user_auth_tuple = authenticator.authenticate(self) …
▶ Local vars
/var/www/cnmb10a/cnmb10a/rest/auth_api/views.py in authenticate
        django_login(request, user) …
▶ Local vars
/var/www/cnmb10a/venv/lib/python3.7/site-packages/django/contrib/auth/__init__.py in login
    if hasattr(request, 'user'): …
▶ Local vars
/var/www/cnmb10a/venv/lib/python3.7/site-packages/rest_framework/request.py in user
                self._authenticate() …
▶ Local vars
/var/www/cnmb10a/venv/lib/python3.7/site-packages/rest_framework/request.py in _authenticate
                user_auth_tuple = authenticator.authenticate(self) …
▶ Local vars
/var/www/cnmb10a/cnmb10a/rest/auth_api/views.py in authenticate
        django_login(request, user) …
▶ Local vars

最后4种方法只是一遍又一遍地重复。

django.contrib.auth.authenticate
将调用后端来尝试对用户进行身份验证。 通过在您的身份验证后端调用它,它将永远循环,因为
django.contrib.auth.authenticate
将调用您的后端,后者将调用函数等等


因此,您需要从尝试覆盖并调用
super().authenticate(request)
而不是
django.contrib.auth.authenticate
django.contrib.auth.authenticate
的身份验证方法调用后端以尝试对用户进行身份验证。 通过在您的身份验证后端调用它,它将永远循环,因为
django.contrib.auth.authenticate
将调用您的后端,后者将调用函数等等


因此,您需要从您试图覆盖并调用
super().authenticate(request)
而不是
django.contrib.auth.authenticate
的身份验证方法中找到问题似乎是
django\u login()
函数调用了我编写的
authenticate()
,然后又调用了
django\u login()
。 解决方案是从
authenticate()
内部删除
django\u login()
,并将其添加到
LoginView()
,因此看起来如下所示:

    authentication_classes = (SessionAuthentication, Authentication)
    permission_classes = (IsAuthenticated,)

    def post(self, request, format=None):
        content = {
            'user': str(request.user),
            'auth': str(request.auth),
        }
        django_login(request, request.user)
        return Response(content)

问题似乎是
django\u login()
函数调用了我编写的
authenticate()
,这反过来又调用了
django\u login()
。 解决方案是从
authenticate()
内部删除
django\u login()
,并将其添加到
LoginView()
,因此看起来如下所示:

    authentication_classes = (SessionAuthentication, Authentication)
    permission_classes = (IsAuthenticated,)

    def post(self, request, format=None):
        content = {
            'user': str(request.user),
            'auth': str(request.auth),
        }
        django_login(request, request.user)
        return Response(content)

你能提供完整的stacktrace错误吗?你能提供完整的stacktrace错误吗?我试过使用
super()。身份验证(请求)
但它告诉我必须重写该方法。但是我不相信这里的问题是
django.contrib.auth.authenticate
方法,就像我注释掉
django.contrib.auth.login
方法一样,它可以工作,但不会在会话中保存用户。我用
login
方法尝试了您的解决方案,但是
super()
没有登录方法。我尝试了使用
super()。验证(请求)
但它告诉我必须重写该方法。但是我不相信这里的问题是
django.contrib.auth.authenticate
方法,就像我注释掉
django.contrib.auth.login
方法一样,它可以工作,但不会在会话中保存用户。我用
login
方法尝试了您的解决方案,但是
super()
没有登录方法。