Compare integer variable in django templates

Ever needed to compare some variable (that may come from db, forms, etc) in django templates with an integer?
Example:

The above example will not work. Django’s template engine interprets the variable as a string.

Workaround for integer comparison:

By using the “add” filter, the variable is transformed into an integer.

  • pedro

    sounds hacky.

  • http://tolerious.com/ tolerious

    you can use {% if some_var == ‘3’ %}

    • http://simionbaws.ro SimionBaws

      It works, but only for equal comparison :)

      • http://tolerious.com/ tolerious

        Yes, but if I want to compare between numbers such as ‘a > b’, any way to do this?

        • http://simionbaws.ro SimionBaws

          {% if a|add:0 > b|add:0 %}

          But don’t write too much logic in templates.
          Data should already come prepared for the template.
          That’s why django does not allow complex comparisons, with “and” and “or” combined.

          • http://tolerious.com/ tolerious

            Oh, yes, it’s so kind of you, thank you for your help~