我收到此错误 rest_framework.request.WrappedAttributeError

Raz*_*han 2 django django-rest-framework

我正在尝试将我的 django 1.9 升级到 django 2.0。它对 GET() 工作正常,但在 POST() 中出现错误。我的views.py是:-

class AccountInfoUpdate(APIView):
    authentication_classes = [IsAuthenticated]

    def post(self, request):
        user = request.user
        user_profile = UserProfile.objects.get(user=user)
        name = False
        contact = False
        if "name" in request.data:
            user_profile.name = request.data.get('name')
            user_profile.save()
            name = True
        if "contact" in request.data:
            user_profile.contact = request.data.get('contact')
            user_profile.save()
            contact = True

        if user_profile.affiliate_code is not None and (name or contact):
            result = service.update_affiliate(user_profile.affiliate_code, name=user_profile.name,
                                                   contact=user_profile.contact)

        return Response({'Message': 'Account info updated successfully!'})
Run Code Online (Sandbox Code Playgroud)

我收到此错误:-

user_auth_tuple = authenticator.authenticate(self)
rest_framework.request.WrappedAttributeError: 'IsAuthenticated' object has no attribute 'authenticate'
Run Code Online (Sandbox Code Playgroud)

如果我'rest_framework.authentication.SessionAuthentication',从 REST_FRAMEWORK 中删除或评论,那么我会收到此错误CSRF Failed: CSRF token missing or incorrect

我尝试permission_classes = [IsAuthenticated]并在 Postman 上休假,但仍然遇到同样的错误。

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

JPG*_*JPG 5

IsAuthenticated是一个Permission Class不是Authentication类。所以它应该是


class AccountInfoUpdate(APIView):
    permission_classes = [IsAuthenticated]
    # your code
Run Code Online (Sandbox Code Playgroud)



UPDATE-1
如何解决CSRF Failed错误

1.在 POSTMAN 中打开一个新选项卡
邮递员截图 2. 提供 URL(1) 3. 转到Authorization选项卡 (2) 并设置,Basic Auth然后提供您的用户名和密码
4. 然后转到Body选项卡,并输入您的 JSON 负载。5.点击send按钮。你去吧