naseeba c
naseeba c

Reputation: 1050

How to get current user data in django

I'm trying to get logged user data but I get an only username

I try these codes

 user= User.objects.get(id=user_id)
 user= User.objects.filter(id=emp_data.user_id).first()
 user =request.user

this 3 query returns username

how can i get user details

Upvotes: 0

Views: 1454

Answers (4)

user19255291
user19255291

Reputation:

You can try:

def user_information(request, pk):
    user_infor = get_objects_or_404(User, id=pk)
    return render(request, 'infor.html', {'user_infor':user_infor})

And in your infor.html you can get user information by:

<p>{{user_infor.user.email}}</p>
<p>{{user_infor.user.username}}</p>

Upvotes: 0

Артем Котов
Артем Котов

Reputation: 99

Try this:

user = User.objects.get(id=user_id)
print(user.__dict__)

This`ll give you all attributes of User class

Upvotes: 2

ruddra
ruddra

Reputation: 52028

Probably you are using print() function to print the object, hence you are seeing the username. This username comes from this implementation (source on GitHub):

def __str__(self):
    return self.get_username()

Where get_username() method returns username, and print executes __str__ function. If you want to see more properties or methods of the object, either use print(dir(user)) or debug using your IDE (or with pdb). You can see the type of object by type(user) function.

Upvotes: 0

pzutils
pzutils

Reputation: 530

These queries don’t return the username they actually return a User object. You can access the object attributes like user.name user.email etc

Upvotes: 0

Related Questions