简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM