0

Seems like elementary question, and yet can't get it work

{% if iterator.next > 10 %}
    Do smth
{% endif %}

Two issues. First, this code just won't work (the code in the if-condition never implemented even when the condition seems to hold true), and second issue - the ">" sign is highlighted as if it where the closing tag of the closest open tag. Any ideas how to fix the first issue and is it all right with second issues ? Maybe there's some elegant syntax that I am missing and that would remove this ambiguity for the text editor ?

djvg
  • 11,722
  • 5
  • 72
  • 103
Edgar Navasardyan
  • 4,261
  • 8
  • 58
  • 121

2 Answers2

1

iterator.next may be a string which would result in the statement being False.

Try creating a custom filter to convert it to an int. For example create "my_filters.py":

# templatetags/my_filters.py
from django import template

register = template.Library()

@register.filter()
def to_int(value):
    return int(value)

Then in your template:

{% load my_filters %}
{% if iterator.next|to_int > 10 %}
   Do smth
{% endif %}

More on custom tags and filters here

I wouldn't worry about the highlighting, this may just be your IDE. I recommend using PyCharm for Django development

sidarcy
  • 2,958
  • 2
  • 36
  • 37
-1

Django's docs says that you can use > with if tag:

{% if somevar < 100 %}
  This appears if variable somevar is less than 100.
{% endif %}

take a look at documentation: https://docs.djangoproject.com/en/1.9/ref/templates/builtins/

maybe you are missing something else?