Django:“django.http.request.RawPostDataException:从请求的数据流中读取后无法访问正文”

时间:2017-02-22 19:51:53

标签: django django-rest-framework

每次我尝试登录此用户时都会收到错误。以下是相关的追溯:

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

这是我的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)

1 个答案:

答案 0 :(得分:1)

这可能是因为你在函数名称中存在冲突。

您可能已将auth.login导入为

from django.contrib.auth import login

同时,您的登录视图名为login。因此冲突。

尝试将导入更改为

from django.contrib import auth

然后将login视图修改为:

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