Search code examples
djangousernametoken

How do i Get the first and second letter of a username in Django <div> {{Post.author.username.0} } </div>


I want to display images according to the user's first and second letter from the username  instead of an image with rounded corners. But

<div> {{Post.author.username.0} } </div>

Returns only the first letter instead. Please help, how do I return 2 letters for example: Ahmed as Ah instead of A.


Solution

  • You can use the |slice template filter [Django-doc] to construct a substring with the first two characters:

    {{ Post.author.username|slice:":2" }}