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

Time:09-20

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='[email protected]', 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.

CodePudding user response:

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.

CodePudding user response:

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

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