Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

Python jinja2 template, how to count a list [duplicate]

Writer Olivia Zamora

So I can't use python len() for a list in the templates like below.

{% if len(alist) == 0 %}
UndefinedError: 'len' is undefined
  1. How can we use python in the templates?

  2. Is passing a param to the template in the def get(self) method the only way to do this?

  3. 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.

0

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