How to get user object from Django DRF when logged in with TemplateView?

Viewed 36

I am begginer in Djnago and logged in to my app with this code:

class LoginHandler(TemplateView):
    def get(self, request, *args, **kwargs):
        user = authenticate(request, email='jaxen@gmail.com', password='123456')
        login(request, user)
        return render(request, "login.html", context={})

But i need to detect logged in user in other app that use DRF.
I don't know how fetch the user.
I tried this code but not worked:

class OtherAppHandler(APIView): 
    def post(self, request):
        print(f"user: {request.user}")
        ...

Thank you.

2 Answers

I think that api is stateless. In api, you need to pass the authorization token from the front end to call API and then get the user from the request.user inside api.

I think you need to fetch the user. Using get method.

def get(self, request):
    user_data = request.GET.get('user', '')
Related