user7693832
user7693832

Reputation: 6849

Can I cut out the string in the Django template?

This is my code in templates:

{% for wine_con in data.winry_consult %}
    <li><a href="/article_list_content-{{win_con.id}}/"><p>{{ win_con.content }}</p><i class="font">&#xe6aa;</i></a></li>
{% endfor %}

I found the win_con.content is too long for my website, I only want 5 count characters, how can I cut out the win_con.content in template?

Upvotes: 15

Views: 14623

Answers (4)

Olaleye Abiola
Olaleye Abiola

Reputation: 77

To cut specific characters do you could use 'cut' which is also part of django template builtins

for example if

{{ file.pdf.name}}

gives 'store/pdfs/verma2010.pdf'

{{ file.pdf.name | cut:'store/pdfs/'}}

Would give 'verma2010.pdf'

Upvotes: 0

PRMoureu
PRMoureu

Reputation: 13327

You can use slice filter:

<li><a href="/article_list_content-{{win_con.id}}/"><p>{{ win_con.content|slice:":5" }}</p><i class="font">&#xe6aa;</i></a></li>

Upvotes: 26

pkisztelinski
pkisztelinski

Reputation: 542

https://docs.djangoproject.com/en/1.11/ref/templates/builtins/#truncatechars should help

{{ value|truncatechars:9 }}

Upvotes: 3

user8060120
user8060120

Reputation:

you may use built-in filter truncatechars

{{ win_con.content|truncatechars:5 }}

Upvotes: 14

Related Questions