【问题标题】:Django: "django.http.request.RawPostDataException: You cannot access body after reading from request's data stream"Django:“django.http.request.RawPostDataException:从请求的数据流中读取后,您无法访问正文”
【发布时间】:2017-02-22 19:51:52
【问题描述】:

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

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)

【问题讨论】:

    标签: django django-rest-framework


    【解决方案1】:

    这可能是因为你的函数名有冲突。

    您可能已经将auth.login 导入为

    from django.contrib.auth import login
    

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

    尝试将导入更改为

    from django.contrib import auth
    

    然后修改login视图为:

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

    【讨论】:

    • 你是我的英雄。是的,我之前遇到过一些类似的问题,所以我不得不导入模块。
    猜你喜欢
    • 2020-11-12
    • 2020-11-08
    • 2019-11-21
    • 2021-03-13
    • 1970-01-01
    • 2023-03-14
    • 2014-02-18
    • 2020-01-29
    • 2017-06-19
    相关资源
    最近更新 更多