[英]jinja2 if statement not working
I have a jinja2 if
statement where I am checking to see if the dictionary item is equal to an id however it never seems to evaluate it correctly or at all. 我有一个jinja2
if
语句,我在其中检查字典项是否等于id,但是它似乎从来没有正确或根本没有对它进行评估。
Here is my if
statement: 这是我的
if
语句:
<select id="deviceTypes" class="inputBoxes" style="height: 25px;">
{% for key, value in deviceTypes.iteritems() %}
{% if deviceTypeID == key %} --> deviceTypeID is defined but this block of code never runs (key is an integer value, it's the id of the option)
<option value="{{key}}" selected>{{deviceTypeID}}</option>
{% else %}
<option value="{{key}}">{{value}}</option>
{% endif %}
{% endfor %}
</select>
Most likely your deviceTypeID
is taken from the request and still a string . 您的
deviceTypeID
很可能是从请求中获取的,并且仍然是字符串 。 Make sure it is an integer: 确保它是整数:
{% if deviceTypeID|int == key %}
or better still, turn it into an integer when you get it from the request. 或更妙的是,从请求中获取时将其转换为整数。 Many web frameworks let you turn a value into an integer when retrieving it;
许多Web框架允许您在检索值时将其转换为整数。 Flask lets you do:
Flask让您可以:
deviceTypeID = request.form.get('deviceTypeID', type=int)
for example. 例如。
Can you try to add 'not' to the check? 您可以尝试在支票上加上“不”吗? This will tell you if the condition is
false
or not. 这将告诉您条件是否为
false
。
{% if not deviceTypeID == key %} {%如果不是deviceTypeID ==键%}
The answer: 答案:
{% if not deviceTypeID == key %}
<option value="{{key}}">{{value}}</option>
{% else %}
<option value="{{key}}" selected>{{deviceTypeID}}</option>
{% endif %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.