2017-08-16 11 views
0

ステータスがpending_approvalまたはscheduledのテーブル行だけが削除ボタンを表示するjinja2を使用してif else条件を作成しようとしています。しかし、テーブル内のすべてのデータがfor loopに表示されているので、条件を満たしていて、すべての行に削除ボタンが付いていて、その逆の場合には問題が発生しています。Jinja2 - 条件が真であれば特にhtmlテーブル行を表示する

すべてのヘルプははるかに以下

を高く評価している私のコードです:

model.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') 

view.py

def my_leaves_view(request): 
    leaves_log = Leave.objects.all().filter(employee=request.user.profile.employee.id) 
    context = {'leaves_log': leaves_log} 
    return render(request, 'hrm/employee/details/my_leaves.html', context) 

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> 

答えて

2

or演算子を正しく使用していません。あなたは

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

bool('any non-empty string')として解釈される

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

を想像することができますので、正しい構文は

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

で常にTrue

され、2つのブール値を分離するために使用されますまたは

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

こんにちは@Nils、ありがとうございました! –

関連する問題