reza_khalafi
reza_khalafi

Reputation: 6534

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

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.

Upvotes: 2

Views: 288

Answers (2)

Bhuban ghimire
Bhuban ghimire

Reputation: 96

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.

Upvotes: 1

Parvez Khan Pathan
Parvez Khan Pathan

Reputation: 14

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

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

Upvotes: 0

Related Questions