The problem with Django templates - {% if object | length> 4%} calls TemplateDoesNotExist: 500.html

My template has the following.

{% block content %} {% for album in albumsList %} {% if fotosList %} <div class="photoalbum-wrapper"> <h3>{{ album.title }}</h3> <ul class="photoalbum"> {% for foto in fotosList %}<li>item</li>{% endfor %} </ul> {% if fotosList|length > 4 %} <a href="#" class="trigger"> <span>&#9660;</span></a> {% endif %} </div> {% endif %} {% endfor %} {% endblock %} 

And he calls TemplateDoesNoteExist: 500.html.

If I write a simple {{ fotoList|length }} , it works fine.

+4
source share
4 answers

Use fotosList.count instead of fotosList|length . You will get the desired result.

+5
source

{% if fotosList|length > 4 %} not a valid tag; you cannot use more or less statements in the Django if tag. (You can use operators in the latest development version, but I assume that you are not using the latest version from the Django SVN repository.)

The reason you get the TemplateDoesNotExist error is because Django throws 500 internal server errors (due to an invalid tag), but you did not specify the 500.html error template, as noted here .

+2
source

FYI , if tags with operators == ,! =, <,>, <=,> = are now supported in the Django development version.

+2
source

This is a very old question. Since newer versions of Django support statements in if-statement , so the following code will work fine:

 {% if fotosList|length > 4 %} {% endif %} 
+2
source

Source: https://habr.com/ru/post/1299586/


All Articles