Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs Django Rest Framework-Request.user始终是匿名用户,Request.POST为空_Angularjs_Django_Authentication_Django Rest Framework - Fatal编程技术网

Angularjs Django Rest Framework-Request.user始终是匿名用户,Request.POST为空

Angularjs Django Rest Framework-Request.user始终是匿名用户,Request.POST为空,angularjs,django,authentication,django-rest-framework,Angularjs,Django,Authentication,Django Rest Framework,我的身份验证/登录视图面临问题。这个系统以前可以工作,但我最近换了一个新服务器,无法修复它 尝试通过身份验证视图登录时,request.user始终是匿名用户,就像我没有输入任何身份验证凭据一样。我尝试记录request.POST,但它似乎是一个空的dict 我这里有一个追踪: Environment: Request Method: POST Request URL: http://45.55.149.3:8000/api/auth/ Django Version: 1.8.3 Pyth

我的身份验证/登录视图面临问题。这个系统以前可以工作,但我最近换了一个新服务器,无法修复它

尝试通过身份验证视图登录时,request.user始终是匿名用户,就像我没有输入任何身份验证凭据一样。我尝试记录request.POST,但它似乎是一个空的dict

我这里有一个追踪:

Environment:


Request Method: POST
Request URL: http://45.55.149.3:8000/api/auth/

Django Version: 1.8.3
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'webapp',
 'rest_framework',
 'djrill')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "/home/appointments-app/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/appointments-app/venv/local/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)
File "/home/appointments-app/venv/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
  71.             return self.dispatch(request, *args, **kwargs)
File "/home/appointments-app/venv/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  456.             response = self.handle_exception(exc)
File "/home/appointments-app/venv/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  453.             response = handler(request, *args, **kwargs)
File "/home/appointments-app/appointments/webapp/views.py" in post
  40.         login(request, request.user)
File "/home/appointments-app/venv/local/lib/python2.7/site-packages/django/contrib/auth/__init__.py" in login
  111.     request.session[SESSION_KEY] = user._meta.pk.value_to_string(user)

Exception Type: AttributeError at /api/auth/
Exception Value: 'AnonymousUser' object has no attribute '_meta'
这里是API auth视图,它失败了:

class AuthView(APIView):
    authentication_classes = (QuietBasicAuthentication,)

    def post(self, request, *args, **kwargs):
        login(request, request.user)
        return Response(OldUserSerializer(request.user).data)

    def delete(self, request, *args, **kwargs):
        logout(request)
        return Response({})
下面是我正在使用的身份验证类:

from rest_framework.authentication import BasicAuthentication

class QuietBasicAuthentication(BasicAuthentication):
    # disclaimer: once the user is logged in, this should NOT be used as a
    # substitute for SessionAuthentication, which uses the django session cookie,
    # rather it can check credentials before a session cookie has been granted.
    def authenticate_header(self, request):
        return 'xBasic realm="%s"' % self.www_authenticate_realm

如果您使用的是Django REST framework的身份验证类,则无需登录用户。Django REST框架将提前对用户进行身份验证,并在此过程中验证凭据

现在通过调用
login
您正试图登录当前用户(
request.user
),并将其与当前请求(
request
)关联。DRF将自动为您执行此操作,并且
请求。如果用户
能够对用户进行身份验证,则它将包含一个
用户
实例;如果无法验证,则它将包含一个
匿名用户
(您看到的)

如果您试图登录用户以获取Django请求(而不是DRF请求),则需要引用存储为
请求的Django请求

login(request._request, request.user)

我不清楚你想做什么。通常,您会使用请求中的信息获取用户,然后调用
login(请求,用户)
让他们登录。调用
login(request,request.user)
没有意义-如果
request.user
不是匿名的,就没有必要让他们登录。@Alasdair似乎我无法访问Ajax请求中提交的凭据。正如我所说,request.POST似乎是空的,request.user也没有包含它们,request.username或request.password也没有。例如,哪些是我在请求中提交的密钥。
request.body
?@Alasdair request.body是空的。恐怕我没有任何其他建议。希望你能设法解决你的问题。