在Django模板语言中快速运行if语句
我正在使用这段代码:
<div class="stream {% if streams.online %}online{% else %}offline{% endif %}">
看起来有点多余。我想运行这段代码
<div class="stream {{ 'online' if stream.online else 'offline' }}">
但是可惜这样不行。有没有更简单、更整洁的方法来实现我想要的效果?
1 个回答
51
我正在使用这段代码:
<div class="stream {% if streams.online %}online{% else %}offline{% endif %}">
看起来有点多余。我想运行这段代码
<div class="stream {{ 'online' if stream.online else 'offline' }}">
但是可惜这样不行。有没有更简单、更整洁的方法来实现我想要的效果?