简体   繁体   English

Jinja 复杂的 if 语句不能正常工作

[英]Jinja complex if statement not working properly

I am working on using if statements in Jinja.我正在努力在 Jinja 中使用 if 语句。 This if statement is designed to see if a dictionary object is equal to another object in the same string value.此 if 语句旨在查看字典对象是否等于同一字符串值中的另一个对象。 The code runs flawlessly in an separate python file.代码在单独的 python 文件中完美运行。 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.如果我是对的,则会抛出错误,因为函数int是未知的。 You can use the jinja filter int to convert the variables to integers.您可以使用 jinja 过滤器int将变量转换为整数。 The statement would then read as follows.然后该声明将如下所示。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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