开发者

Django templates problem — {% if object|length > 4 %} raises TemplateDoesNotExist: 500.html

I have the following in my template.

{% block content %}
    {% for album in albumsList %}
        {% if fotosList %}
            <div class="photoalbum-wrapper">
                <h3>{{ album.title }}</h3>
                <ul class="photoalbum">
                    {% for foto in fotosLi开发者_开发问答st %}<li>item</li>{% endfor %}
                </ul>
                {% if fotosList|length > 4 %}
                    <a href="#" class="trigger">больше <span>&#9660;</span></a>
                {% endif %}
            </div>
        {% endif %}  
    {% endfor %}
{% endblock %}

And it raises TemplateDoesNotExist: 500.html.

If i write simple {{ fotoList|length }} it works okay.


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

{% if fotosList|length > 4 %}

{% endif %}


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


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


{% if fotosList|length > 4 %} is not a valid tag; you can't use greater than/less than operators in the Django if tag. (You can use operators in the latest development release, but I'm assuming you're not using the latest version from Django's SVN repository.)

The reason you get the TemplateDoesNotExist error is because Django is throwing a 500 Internal Server Error (due to the invalid tag), but you haven't supplied a 500.html error template, as noted here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜