Home > Net >  Jinja complex if statement not working properly
Jinja complex if statement not working properly

Time:06-23

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]) %}

CodePudding user response:

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) %}
  • Related