猿问

使用 Python 编写 HTML 中的 if 语句

我必须在 Python 和 HTML 中使用“或”。


 <div>

     {% if imp[3][0] == 'abc' || imp[3][0] == 'def' %}

         <p>yes</p>

     {% endif %}

 </div>

我收到一个错误:


jinja2.exceptions.TemplateSyntaxError: expected token 'name', got '|'

但是, && 可以工作。反正?


宝慕林4294392
浏览 453回答 1
1回答

BIG阳

您应该使用关键字or而不是布尔运算符||<div>&nbsp; &nbsp; &nbsp;{% if imp[3][0] == 'abc' or imp[3][0] == 'def' %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<p>yes</p>&nbsp; &nbsp; &nbsp;{% endif %}</div>当然in在这里使用会更好:<div>&nbsp; &nbsp; &nbsp;{% if imp[3][0] in ('abc', 'def') %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<p>yes</p>&nbsp; &nbsp; &nbsp;{% endif %}</div>
随时随地看视频慕课网APP

相关分类

Python
我要回答