[Fixed]-Truncate Text But in Backward Direction in Django

1👍

You can write your own filters.

@register.filter("truncate_chars")
def truncate_chars(value, max_length):
    if len(value) > max_length:
        #will return last characters if max_length<0
        truncd_val = value[max_length:]
        if not len(value) == max_length+1 and value[max_length+1] != " ":
            truncd_val = truncd_val[:truncd_val.rfind(" ")]
        return  truncd_val + "..."
    return value

and then

{{ myString|truncate_chars:-9 }}

Look at here.

👤Aram

Leave a comment