B
B
bituke2022-04-06 17:52:04
Django
bituke, 2022-04-06 17:52:04

How to use authorization token in normal views?

Good afternoon!
vue+django client/server application
I have a custom view that allows users to like articles.

def article_like(request):
  print(request.user)
  print(request.headers['Authorization'])
  return JsonResponse({'status': 'ok'})


The problem is that I can't get request.user even though the token is in the Authorization header.
I registered all the configs, there is such an authorization class:
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
    ),
}

I can get data about users via rest api using this token, but I can't access request.user. How can I do that? Or do you need to send data about username from the front and get the user object through it? Thanks for the answer.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Nesterov, 2022-04-06
@bituke

No way.
Standard Django methods do not and cannot know what resources third-party modules operate on (at least not without crutches).
There is only one solution here - do not reinvent the wheel and use ready-made solutions from DRF.
If you really want to use "standard" views, then use Function Based Views

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question