Stack Overflow Asked by Đỗ Nguyên Tùng on December 24, 2020
In a template, I have a variable of type list called "participants". I want to check if the length of the list is equal to 2, for example. I tried the following:
{{ participants | json_script:"participants"}}
{% if participants|length==2 %}
.....
{% endif %}
However, this does not work. The error I get is:
TemplateSyntaxError at /chat/lobby/
Could not parse the remainder: ‘==2’ from ‘participants.count==2’
Can someone point out a way to access a list’s length in a template’s code block? thank you for your time and consideration!
The problem is the (lack of) spacing around the ==
part. If you rewrite this to
{% if participants|length == 2 %}
…
{% endif %}
the template parser will no longer error.
That being said, a template is used for rendering logic, and while it is hard to tell, it looks that this is more business logic, which, as @MeL says belongs in the views.
Correct answer by Willem Van Onsem on December 24, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP