Python jinja2 template, how to count a list [duplicate]
Olivia Zamora
So I can't use python len() for a list in the templates like below.
{% if len(alist) == 0 %}
UndefinedError: 'len' is undefinedHow can we use python in the templates?
Is passing a param to the template in the def get(self) method the only way to do this?
Anyone know some good resources on how to use jinja2 with it comes to templating? like what methods can you use and the syntactic difference between python and jinja2.
3 Answers
If you do a quick search in the template documentation you will quite soon find the length filter.
As for the rest, read the documentation.
4{% if alist |length ==0 %} or {% if alist |count ==0 %}i Solve it with that way!!
{% if alist.count() == 0 %}That should solve your problem.
You can check out this link.
2