Django Rest Framework - Request.user is always an anonymous user and request.POST is empty

I am having a problem with my authentication / login. This system worked before, but recently I switched to a new server and cannot fix it.

When trying to log in through auth-view request.user is always an anonymous user, as if I did not upload any credentials. I tried logging the .POST request, but it seems empty.

I have a trace here:

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' 

Here I have an API authenticator that does not work:

 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({}) 

below is the authentication class I'm using:

 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 
+5
source share
1 answer

If you use the authentication classes of the Django REST framework, you do not need to register the user. Before the time runs out, the user will authenticate with the Django REST framework, and credentials will be verified in the process.

Right now, by calling login , you are trying to log in to the current user ( request.user ) and associate them with the current request ( request ). DRF will do this automatically for you, and request.user will contain an instance of User if it can authenticate the user and AnonymousUser (what you see) if it is not capable.

If you are trying to register a user for a Django request (and not a DRF request that is different from ), you need to contact Django, which is stored as request._request .

 login(request._request, request.user) 
+1
source

All Articles