user16355394
user16355394

Reputation: 31

Jinja complex if statement not working properly

I am working on using if statements in Jinja. This if statement is designed to see if a dictionary object is equal to another object in the same string value. The code runs flawlessly in an separate python file. An example is listed below and thanks for any help in advance!

Runs perfectly:

dictionary = {'test': '3/3'}

if int(dictionary['test'].split('/')[0]) == int(dictionary['test'].split('/')[1]):
    print('true')
else:
    print('no')
    

Does not run and crashes the server: (item is a variable accessible to the program)

{% elif int(dictionary[item].split('/')[0]) == int(dictionary[item].split('/')[1]) %}

Upvotes: 0

Views: 287

Answers (1)

Detlef
Detlef

Reputation: 8562

If I'm right, the error is thrown because the function int is not known. You can use the jinja filter int to convert the variables to integers. The statement would then read as follows.

{% elif (dictionary[item].split('/')[0]|int) == (dictionary[item].split('/')[1]|int) %}

Upvotes: 2

Related Questions