Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: How to get user full name from user id

My model:

author=models.ForeignKey(User)

User id (ex. 174948) is saved to column author_id. In the template I show user id with {{post.author}} I want also to show full name for that particular user. How to do that in the template?

like image 699
user3760940 Avatar asked Mar 05 '15 00:03

user3760940


3 Answers

By using the method that does it.

{{post.author.get_full_name}}
like image 55
Ignacio Vazquez-Abrams Avatar answered Sep 21 '22 12:09

Ignacio Vazquez-Abrams


It depends if the user object has the first and/or last name fields set.

If not (for example a new user was just created), then calling {{ post.author.get_full_name }} may return an empty string.

This method is more reliable:

{{ post.author.get_full_name|default:post.author.username }}
like image 33
robvdl Avatar answered Sep 21 '22 12:09

robvdl


Another way

{{post.author.first_name}} {{post.author.last_name}} 
like image 20
levi Avatar answered Sep 23 '22 12:09

levi