【问题标题】:RawPostDataException: You cannot access body after reading from request's data streamRawPostDataException:从请求的数据流中读取后,您无法访问正文
【发布时间】:2016-09-13 05:27:20
【问题描述】:

我在 Google Cloud 上托管了一个网站,我让一切正常运行,然后突然间我开始收到此错误..

01:16:22.222
Internal Server Error: /api/v1/auth/login/ (/base/data/home/apps/s~crs-portal/1.395605052160854207/lib/django/core/handlers/exception.py:124)
Traceback (most recent call last):
  File "/base/data/home/apps/s~crs-portal/1.395605052160854207/lib/django/core/handlers/exception.py", line 39, in inner
    response = get_response(request)
  File "/base/data/home/apps/s~crs-portal/1.395605052160854207/lib/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/base/data/home/apps/s~crs-portal/1.395605052160854207/lib/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/base/data/home/apps/s~crs-portal/1.395605052160854207/lib/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/base/data/home/apps/s~crs-portal/1.395605052160854207/lib/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/base/data/home/apps/s~crs-portal/1.395605052160854207/lib/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/base/data/home/apps/s~crs-portal/1.395605052160854207/lib/rest_framework/views.py", line 474, in dispatch
    response = self.handle_exception(exc)
  File "/base/data/home/apps/s~crs-portal/1.395605052160854207/lib/rest_framework/views.py", line 434, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/base/data/home/apps/s~crs-portal/1.395605052160854207/lib/rest_framework/views.py", line 471, in dispatch
    response = handler(request, *args, **kwargs)
  File "/base/data/home/apps/s~crs-portal/1.395605052160854207/authentication/views.py", line 42, in post
    data = json.loads(request.body)
  File "/base/data/home/apps/s~crs-portal/1.395605052160854207/lib/rest_framework/request.py", line 359, in __getattribute__
    return getattr(self._request, attr)
  File "/base/data/home/apps/s~crs-portal/1.395605052160854207/lib/django/http/request.py", line 263, in body
    raise RawPostDataException("You cannot access body after reading from request's data stream")
RawPostDataException: You cannot access body after reading from request's data stream

我不知道这意味着什么,而且没完没了的谷歌搜索也没有解决我的问题..

这是可能相关的代码:

views.py

class LoginView(views.APIView):
    def post(self, request, format=None):
        data = json.loads(request.body)

        email = data.get('email', None)
        password = data.get('password', None)

        account = authenticate(email=email, password=password)

        if account is not None:
            if account.is_active:
                login(request, account)

                serialized = AccountSerializer(account)

                return Response(serialized.data)
            else:
                return Response({
                    'status': 'Unauthorized',
                    'message': 'This account has been disabled.'
                }, status=status.HTTP_401_UNAUTHORIZED)
        else:
            return Response({
                'status': 'Unauthorized',
                'message': 'Username/password combination invalid.'
            }, status=status.HTTP_401_UNAUTHORIZED)

serializer.py

class AccountSerializer(serializers.ModelSerializer):
    password = serializers.CharField(write_only=True, required=False)
    confirm_password = serializers.CharField(write_only=True, required=False)

    class Meta:
        model = Account
        fields = ('id', 'email', 'username', 'created_at', 'updated_at', 'full_name', 'password', 'confirm_password')
        read_only_fields = ('created_at', 'updated_at',)

        def create(self, validated_data):
            return Account.objects.create(**validated_data)

        def update(self, instance, validated_data):
            instance.username = validated_data.get('username', instance.username)

            instance.save()

            password = validated_data.get('password', None)
            confirm_password = validated_data.get('confirm_password', None)

            if password and confirm_password and password == confirm_password:
                instance.set_password(password)
                instance.save()

            update_session_auth_hash(self.context.get('request'), instance)

            return instance

【问题讨论】:

    标签: python django django-rest-framework


    【解决方案1】:

    这是因为您试图从body访问数据

    使用 -> data = json.loads(request.data)

    【讨论】:

    • 嘿,感谢您的回复,但我想通了!它不起作用,因为我发现了一个小错误,即使我已经登录也可以访问登录页面,因此错误是由于尝试再次登录引起的。如果尝试访问登录页面,我通过重定向到主页解决了这个问题
    • AttributeError: 'WSGIRequest' object has no attribute 'data'
    【解决方案2】:

    使用request.data 而不是request.body。

    request.data 不再读取数据流。

    【讨论】:

    • 我得到这个错误:'WSGIRequest'对象没有属性'data'
    • @Ulvi data 是 DRF 请求的一个属性,vanilla Django 没有。
    【解决方案3】:

    嘿,感谢您的回复,但我想通了!它不起作用,因为我发现了一个小错误,即使我已经登录也可以访问登录页面,因此错误是由于尝试再次登录引起的。如果尝试访问登录页面,我通过重定向到主页解决了这个问题

    【讨论】:

      猜你喜欢
      • 2019-11-21
      • 2020-11-08
      • 2021-03-13
      • 2020-11-12
      • 2023-03-14
      • 1970-01-01
      • 2014-02-18
      • 2020-01-29
      • 2017-06-19
      相关资源
      最近更新 更多