Nani
Nani

Reputation: 5

How can I get the username of the logged in user in Django?

I'M new to Django, looking to get first name and last name of the logged-in user in Django, login method am using LDAP authentication.

when i login as admin i was able to see the LDAP user in User information tab.

Tried request.user.first_name didn't work, is there any other method to get the details?

Upvotes: 0

Views: 482

Answers (2)

Mahmudul Alam
Mahmudul Alam

Reputation: 153

There is two method in AbstractUser class, you can use this

request.user.get_full_name()
request.user.get_short_name()

get_short_name will return first_name

Upvotes: 0

SAI SANTOSH CHIRAG
SAI SANTOSH CHIRAG

Reputation: 2084

You can use this method to get first name, last name and all details of the logged in user.

from django.contrib.auth.models import User
first_name = User.objects.get(username=request.user).first_name
last_name = User.objects.get(username=request.user).last_name

In the same way you can get any attribute of the user. For logged in users, you have to use the keyword request.user.

Upvotes: 1

Related Questions