如果条件是tru,Jinja2在特定的html表行中显示delete按钮

2024-05-21 05:01:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用jinja2创建一个if-else条件,其中只有状态为pending_approval或{}的表行将在其旁边显示一个删除按钮。但是我很难搞清楚,因为表中的所有数据都显示在for loop中,所以如果条件为真,并且所有行都有delete按钮,反之亦然。在

非常感谢任何帮助

以下是我的代码:

在模型.py在

class Leave(models.Model):
    employee = models.ForeignKey(Employee, on_delete=models.CASCADE, related_name='+')
    type = models.ForeignKey(LeavesType, on_delete=models.CASCADE, related_name='+')
    status = (('cancelled', 'Cancelled'),
              ('taken', 'Taken'),
              ('pending_approval', 'Pending Approval'),
              ('scheduled', 'Scheduled'),
              ('weekend', 'Week End'),
              ('public_holiday', 'Public holiday'),
              )
    status = models.CharField(max_length=50, choices=status, default='pending_approval')

在视图.py在

^{pr2}$

html格式

<table id="Log" class="display table table-hover table-responsive leaves-table">
<thead>
    <tr>
        <th class="small text-muted text-uppercase"><strong>Leave Type</strong></th>
        <th class="small text-muted text-uppercase"><strong>Status</strong></th>
        <th class="small text-muted text-uppercase"><strong></strong></th>
    </tr>
</thead>
<tbody>
{% for field in leaves_log %}
    <tr>
        <td>{{field.type}}</td>
        <td><img class="media-object img-circle status-icon-size" src="/media/dashboard/ui/file_status/{{field.status}}.png" style="display: inline-block; height: 24px; margin-right: 10px;">{{field.status}}</td>
        <td><div class="btn-group">
            {% if field.status == 'pending_approval' or 'scheduled'%}
                <button type="button" class="btn btn-default btn-xs dropdown-toggle active" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Action <span class="caret"></span>
                </button>
                <ul class="dropdown-menu dropdown-menu-right">
                    <li onclick="delete_leaves();">
                        <a href="/hrm/employee/{{field.id}}/delete/" onclick="return confirm('Are you sure you want to delete this item?');">
                            <i class="fa fa-fw fa-trash text-gray-lighter m-r-1"></i>Withdraw
                        </a>
                    </li>
                </ul>
            </div>
        </td>
        {% else %}
        <td></td>
        {% endif %}
    </tr>
</tbody>

Tags: textfieldmodelsstatustabledeletetrclass
1条回答
网友
1楼 · 发布于 2024-05-21 05:01:43

您没有正确使用or运算符。它用于分隔两个布尔值,因此您可以想象

{% if field.status == 'pending_approval' or 'scheduled' %}

被解释为

^{pr2}$

并且bool('any non-empty string')总是True

正确的语法是

{% if field.status == 'pending_approval' or field.status == 'scheduled' %}

或者

{% if field.status in ['pending_approval', 'scheduled'] %}

相关问题 更多 >