如何将Django模板标签用于forloop.counter?

Mik*_*keN 3 django django-templates

我想要Django模板页面中的“ ifgt”模板标签的效果:

{%ifgt forloop.counter 10%}<!---special greater than 10 code--!>{%endif%}
Run Code Online (Sandbox Code Playgroud)

use*_*843 5

如果需要大于,可以使用以下简单代码段(将其放入app / templatetags / greaterthan.py中):

from django import template
register = template.Library()

@register.filter
def gt(a, b):
    return a > b
Run Code Online (Sandbox Code Playgroud)

并在模板中:

{% load greterthan %}
{% if forloop.counter|gt:10 %}...{% endif %}
Run Code Online (Sandbox Code Playgroud)