[Django]-Want to display the first character of lastname in django templates

3๐Ÿ‘

โœ…

You can write a custom template filter for it.

from django import template

register = template.Library() 

@register.filter
def last_name_initial(value):
    """ 
    Returns the first character of lastname in lowercase for a given name
    """
    last_name = value.split()[-1] # get the last name from value
    return lastname[0].lower() # get the first letter of last name in lower case

In the template, you can use it like:

{{ name|last_name_initial }}
๐Ÿ‘คRahul Gupta

Leave a comment