I am trying to use Token Authentication from Django Rest Framework.

I followed all the instruction such as:

1. Adding these lines into installed apps:

'rest_framework',
'rest_framework.authtoken',

2. Adding statement into settings:

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

I am sending HTTP request with header 'Authorization': 'Token token_name', where token_name is token of corresponding user which I can see in a database.

On a server, request.user still returns AnonymousUser and request.user.is_authenticated is False. What can be the issue ?

This is the corresponding view code:

@csrf_exempt
def test(request):
    print(request.user.is_authenticated)
    return HttpResponse('ok')

This is corresponding line from urlpatterns:

path('test/', views.test)

0 Answers