Search code examples
pythondjango-rest-frameworkattributesdjango-rest-framework-simplejwt

I keep getting this error in my code. Why does this keep happening? type object 'RefreshToken' has no attribute 'for_user'


i am trying to create a login view using django rest framework simplejwt and i am using this function to create tokens

from rest_framework_simplejwt.tokens import RefreshToken

def get_tokens_for_user(user):
    refresh = RefreshToken.for_user(user)

    return {
        'refresh': str(refresh),
        'access': str(refresh.access_token),
    }

however /i keep getting this error

     type object 'RefreshToken' has no attribute 'for_user'

Solution

  • for generating a token for user you have to use Token not RefreshToken.

    from rest_framework_simplejwt.tokens import Token
    
    def get_tokens_for_user(user):
        refresh = Token.for_user(user)
    
        return {
            'access': str(refresh)
        }