shank sharma
shank sharma

Reputation: 39

Using Django's "truncatechars" to get last n characters of a word

While I was learning Django Templates, I came to know that we can use Django's built in template's truncatechars function to get first n characters of a world like this;

Here, data = Stackoverflow.com

{{ data|truncatechars:9 }} # It would print "Stackover.."

But what should I do If I need last 9 characters of a word like '..rflow.com'

Is there built in function in django template which can give me the inverse of truncatechars:9 ?

Upvotes: 0

Views: 1252

Answers (1)

Mahammadhusain kadiwala
Mahammadhusain kadiwala

Reputation: 3635

You can achieve those things like this...

{% block body %}
     <div class="container-fluid">
        
        <div><strong>Output-1 =</strong> {{a|truncatechars:9}}</div>  <!-- Using Only (truncatechars) -->
        <div><strong>Output-2 =</strong> {{a|slice:'9:'}}</div>  <!-- Using string (slicing) -->
        <div><strong>Output-3 =</strong> {{a|slice:'::-1'|truncatechars:9|slice:'::-1'}}</div> <!-- Using string (slicing + truncatechars) -->

     </div>
{% endblock body %}

Browser Output

enter image description here

Upvotes: 3

Related Questions