【问题标题】:I am getting this error rest_framework.request.WrappedAttributeError我收到此错误 rest_framework.request.WrappedAttributeError
【发布时间】:2018-12-23 15:02:56
【问题描述】:

我正在尝试将我的 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!'})

我收到此错误:-

user_auth_tuple = authenticator.authenticate(self)
rest_framework.request.WrappedAttributeError: 'IsAuthenticated' object has no attribute 'authenticate'

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

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

【问题讨论】:

    标签: django django-rest-framework


    【解决方案1】:

    我也收到此错误“rest_framework.request.WrappedAttributeError: 'IsAuthenticated' object has no attribute 'authenticate'”错误,方式如下:

    我的登录代码有:

    def post(self, request,):
            username = request.data.get("username")
            password = request.data.get("password")
            user = authenticate(username=username, password=password)
    

    当我遇到错误时,REST_FRAMEWORK 在 settings.py 中包含以下内容:

    REST_FRAMEWORK = {
        'DEFAULT_AUTHENTICATION_CLASSES': (
            'rest_framework.authentication.TokenAuthentication',
            'rest_framework.authentication.SessionAuthentication',
        ),  
    }
    

    后来我也添加了以下内容,我的错误消失了:

    REST_FRAMEWORK = {
        ......
        'DEFAULT_PERMISSION_CLASSES': (
            'rest_framework.permissions.IsAuthenticated',
        )   
    }
    

    您可以尝试并提供反馈。

    【讨论】:

      【解决方案2】:

      "CSRF Failed: CSRF token missing or incorrect." 问题已由前端站点解决,但在邮递员方面仍无法正常工作。无论如何,经过长时间的 RND,我的问题通过 link 前端解决了。

      【讨论】:

        【解决方案3】:

        IsAuthenticatedPermission Class 而不是 Authentication 类。所以应该是

        
        class AccountInfoUpdate(APIView):
            permission_classes = [IsAuthenticated]
            # your code



        UPDATE-1
        如何解决CSRF Failed 错误

        1. 在 POSTMAN 中打开一个新标签
        2. 提供网址(1) 3. 转到Authorization tab(2) 并设置Basic Auth 然后提供您的用户名和密码
        4. 然后转到Body 选项卡,输入您的 JSON 有效负载。 5.点击send按钮。给你

        【讨论】:

        • 我试过了,但它没有工作给我这个错误CSRF Failed: CSRF token missing or incorrect.
        • 你试过these solutions
        • 是的,但是我没有得到任何东西来解决我的问题我遇到了同样的错误。
        • 无论如何你能展示你是如何发布数据的,以及你的 urls.py 吗?
        • 我的 url.py 是:- url(r'^v1/account/info/update/$', AccountInfoUpdate.as_view()),
        猜你喜欢
        • 2013-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-04
        • 2012-05-17
        • 2016-07-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多