Wagh
Wagh

Reputation: 4306

Want to display the first character of word in django templates

I want to display first character of user in django templates. I want to make a image of user first character like caption or avatar

like this https://meta.discourse.org/t/switch-from-gravatar-to-html-css-letters-for-no-avatar-users/15336

<p>{{postone.user}}</p>

so i will get result like any name "Gaurav" or "Amit" but i just want first letter of them like "G" or "A" So i can display it like image of first letter.

Upvotes: 17

Views: 21009

Answers (2)

Zac
Zac

Reputation: 411

try <p>{{ postone.user.first_name.0 }}</p>

Upvotes: 29

Alex Lisovoy
Alex Lisovoy

Reputation: 6065

Solution with using templates tags(but for me the answer from @Kasra more preferable):

First letter:

{{ postone.user|make_list|first }}

Word without first:

{{ postone.user|make_list|slice:'1:'|join:'' }}

Upvotes: 39

Related Questions