rocky raccoon
rocky raccoon

Reputation: 544

Django: "django.http.request.RawPostDataException: You cannot access body after reading from request's data stream"

I keep getting an error every time I try to log this user in. Here are the relevant tracebacks:

AttributeError: 'Request' object has no attribute 'body'

During handling of the above exception, another exception occurred:
raise RawPostDataException("You cannot access body after reading from request's data stream")
django.http.request.RawPostDataException: You cannot access body after reading from request's data stream

This is inside my views.py:

@api_view(['POST'])
def login(request):

    email = request.POST['email']
    password = request.POST['password']
    user = authenticate(email=email, password=password)
    if user is not None:
        login(request)
        print("Breakpoint")
        return Response(status=status.HTTP_200_OK)
    print('something went wrong')
    return Response(status=status.HTTP_401_UNAUTHORIZED)

Upvotes: 1

Views: 1909

Answers (1)

narendra-choudhary
narendra-choudhary

Reputation: 4818

It's probably because you're having conflicts in function names.

You probably have imported auth.login as

from django.contrib.auth import login

At the same time, your login view is named login. Hence the conflict.

Try changing import to

from django.contrib import auth

and then modify login view as:

if user is not None:
    auth.login(request)

Upvotes: 1

Related Questions