Reputation: 1918
I need to get the first name and last name of a user for a function in my views How do I do that? What is the syntax for it? I used request.user.email for the email, but I don't know see an option for a first name or last name. How do I go about doing this?
Should I import the model into the views and then use it?
Upvotes: 9
Views: 24219
Reputation: 174624
There are multiple ways to get the information you require.
request.user.get_full_name()
, returns the first name and last name, separated by a space.
request.user.get_short_name()
, returns just the first name
You can also access the attributes directly, with request.user.first_name
and request.user.last_name
, but it is preferred to use the methods as the attributes may change in future versions.
The django user model has a rich set of methods, which you can read about in the reference for the auth
application.
Upvotes: 21
Reputation: 5115
You want the first_name
and last_name
attributes: request.user.first_name
and request.user.last_name
Upvotes: 4