我收到此错误rest_framework.request.WrappedAttributeError

时间:2018-07-16 06:05:24

标签: 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!'})

我收到此错误:-

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上放松了,但仍然遇到相同的错误。

enter image description here

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:2)

IsAuthenticated Permission Class而非Authentication类。所以应该是


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



UPDATE-1
如何解决CSRF Failed错误

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

答案 1 :(得分: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 :(得分:0)

public class MyXAxisValueFormatted implements IAxisValueFormatter{ private String[] mValues; public MyXAxisValueFormatted(String[] values){ this.mValues = values; } @Override public String getFormattedValue(float value, AxisBase axis) { return mValues[(int) value]; } } 问题已由前端站点解决,但仍无法在邮递员方面使用。无论如何,经过漫长的RND之后,我的问题通过link从前端解决了。

相关问题