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.
Use {{ obj | capfirst }} to make uppercase the first character only. Using {{ obj | title }} makes it Camel Case.
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.
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.
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.
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:'' }}
try
<p>{{ postone.user.first_name.0 }}</p>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With