Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply multiple filters on a Django template variable?

Tags:

python

django

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?

like image 797
Tommy Avatar asked Jun 08 '11 15:06

Tommy


People also ask

What does the Django template filter pluralize do?

In Sendwithus you can use the pluralize filter to change the suffix of a word. Like Django's pluralize filter it returns a plural suffix if the first argument is an integer greater than 1 or a sequence with more than 1 item. If a plural suffix isn't specified pluralize defaults to 's'.

What does {% %} mean in Django?

{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.

What is Forloop counter in Django?

Django for loop counter All the variables related to the counter are listed below. forloop. counter: By using this, the iteration of the loop starts from index 1. forloop. counter0: By using this, the iteration of the loop starts from index 0.


1 Answers

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 %} 
like image 81
Suchan Lee Avatar answered Oct 05 '22 00:10

Suchan Lee