Tommy
Tommy

Reputation: 1767

How to apply multiple filters on a Django template variable?

For me this works:

{{ game.description|safe }}

But this fails:

{{ game.description|safe|slice:"65" }}

Is there a way to apply two or more filters on a variable in Django templates?

Upvotes: 44

Views: 33183

Answers (4)

dmoxy
dmoxy

Reputation: 107

change

{{ game.description|safe|slice:"65" }}

to

{{ game.description|safe|slice:":65" }}

you are missing the colon

Upvotes: 3

Suchan Lee
Suchan Lee

Reputation: 607

Although it's quite past when the OP posted the question, but for other people that may need the info, this seems to work well for me:

You can rewrite

{{ game.description|safe|slice:"65" }}

as

{% with description=game.description|safe %}
{{description|slice:"65"}}
{% endwith %}

Upvotes: 46

mynameiscoffey
mynameiscoffey

Reputation: 15982

Is description an array or a string?

If it is a string, you might want to try truncatewords (or truncatewords_html if the description can contain HTML),

{{ game.description|safe|truncatewords:65 }}

Reference: Built-in filter reference, truncatewords.

(I'm new to Django so my apologies if slice works on strings.)

Upvotes: 11

Rene de la garza
Rene de la garza

Reputation: 1269

This may work:

{% filter force_escape|lower %}
    This text will be HTML-escaped, and will appear in all lowercase.
{% endfilter %}

Reference: Built-in tag reference, filter.

Upvotes: 0

Related Questions