Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 966
Wagh Avatar asked Sep 26 '14 11:09

Wagh


People also ask

How do I make the first letter capital in Django?

Use {{ obj | capfirst }} to make uppercase the first character only. Using {{ obj | title }} makes it Camel Case.

What does {{ name }} this mean in Django templates?

8. What does {{ name }} this mean in Django Templates? {{ name }} will be the output. It will be displayed as name in HTML. The name will be replaced with values of Python variable.

What does {% %} mean in Django?

This tag can be used in two ways: {% extends "base.html" %} (with quotes) uses the literal value "base.html" as the name of the parent template to extend. {% extends variable %} uses the value of variable . If the variable evaluates to a string, Django will use that string as the name of the parent template.

What is with tag in Django template?

The template tags are a way of telling Django that here comes something else than plain HTML. The template tags allows us to to do some programming on the server before sending HTML to the client.


2 Answers

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:'' }}
like image 172
Alex Lisovoy Avatar answered Sep 21 '22 02:09

Alex Lisovoy


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

like image 40
Zac Avatar answered Sep 18 '22 02:09

Zac