3

I am trying to upgrade my django 1.9 to django 2.0. It is working fine for GET() but I am getting error in POST(). My views.py is:-

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

I am getting this error:-

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

If I removed or comment 'rest_framework.authentication.SessionAuthentication', from REST_FRAMEWORK then I am getting this error CSRF Failed: CSRF token missing or incorrect.

I tried permission_classes = [IsAuthenticated] and did fallow on Postman but still getting same error.

enter image description here

enter image description here

enter image description here

Razia Khan
  • 493
  • 7
  • 14

3 Answers3

8

IsAuthenticated is a Permission Class not an Authentication class. So it should be as


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



UPDATE-1
How to resolve CSRF Failed error

1. Open a new tab in POSTMAN
POSTMAN Screenshot 2. Provide URL(1) 3. Go to Authorization tab(2) and setect Basic Auth then provide your username and password
4. Then go to Body tab, and enter your JSON payload. 5. Hit send button. There you go

JPG
  • 82,442
  • 19
  • 127
  • 206
  • I tried it but its not working it given me this error `CSRF Failed: CSRF token missing or incorrect.` – Razia Khan Jul 16 '18 at 06:14
  • Did you tried [these solutions](https://www.google.co.in/search?q=CSRF+Failed:+CSRF+token+missing+or+incorrect+site:stackoverflow.com&sa=X&ved=2ahUKEwiDprOP_KLcAhUDtI8KHSSBDKsQrQIoBDAAegQIABAN&biw=1366&bih=650) – JPG Jul 16 '18 at 06:16
  • Yes, but i am not getting anythings to resolved my problem I am getting same error. – Razia Khan Jul 16 '18 at 06:17
  • Anyway can you show how you are posting the data, and your urls.py ? – JPG Jul 16 '18 at 06:17
  • my url.py is:- `url(r'^v1/account/info/update/$', AccountInfoUpdate.as_view()),` – Razia Khan Jul 16 '18 at 06:18
  • and How you are POST your data? – JPG Jul 16 '18 at 06:21
  • Updated the answer. (**Point 1** is the mandatory step, don't miss it) – JPG Jul 16 '18 at 06:42
  • why Authorization? which username and password I need to put ? Its working without this when I hit GET(). Like login, only its not working when I tried to update user name in POST(). I am not getting logged in user through `request.user`. Its working fine in django 1.9 – Razia Khan Jul 16 '18 at 06:56
  • As I said erlier, you are added a `pemission_class` so, you have to put your credentials while accessing corresponding API. The choices of username and password to be used depends on you/your application, for instance you can use **superuser**'s username ans password there – JPG Jul 16 '18 at 07:01
  • In `Header` only Content-Type = application/json – Razia Khan Jul 16 '18 at 08:50
  • In your screenshot, there are 3 entries...Can you show add the Headers screenshot? – JPG Jul 16 '18 at 09:10
  • That **Authorization** must be applied. – JPG Jul 16 '18 at 10:10
  • Mark the answer as *Accepted* if my answer helpfull – JPG Jul 16 '18 at 10:49
1

I was also getting this error "rest_framework.request.WrappedAttributeError: 'IsAuthenticated' object has no attribute 'authenticate'" error in following manner:

my login code was having:

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

While i was getting error, REST_FRAMEWORK contained following in settings.py:

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

Later i added following also and my error was gone:

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

You can try and give feedback please.

Subhasish Paul
  • 211
  • 3
  • 7
0

"CSRF Failed: CSRF token missing or incorrect." problem is solved by front end site but still its not working in postman side. Anyway, my problem is solved frond-end side by this link after long RND.

Razia Khan
  • 493
  • 7
  • 14