如果两个表中存在相同的 ID,则显示编辑按钮,否则在 Django 中显示分配按钮



获取所有员工配置文件表ID并使用员工流程检查ID,如果ID匹配则在模板中显示编辑按钮,否则显示分配按钮。

Views.py

def Employee(request):
emp = Emp_Profile.objects.filter(is_active=True)
emptable = Emp_Profile.objects.values_list('id')
print(emptable)
empprocess = Emp_Process.objects.values_list('username_id').distinct()
print(empprocess)
obj = {}
for i in range(len(empprocess)):
obj[i] = empprocess[i]
return render(request, 'employee.html',{'list' : emp,'empprocess':empprocess,'obj':obj})

模板

{% for list in list %}
{% if  obj != list.id %}
<td>
<a href="/view_client_process/{{ list.id }}"><button
class="btn btn-info">Edit</button></a>
</td>
{% else %}
<h6>welcome</h6>
<td>
<a href="/view_client_process/{{ list.id }}"><button
class="btn btn-info">Assign</button></a>
</td>
{% endif %}
{% endfor %}

您可以构造一组username_id并将其传递给您的模板:

def Employee(request):
empS = Emp_Profile.objects.filter(is_active=True)
empprocess = set(Emp_Process.objects.values_list('username_id', flat=True).distinct())
return render(request, 'employee.html', {'emps' : emps, 'empprocess': empprocess })

在模板中,我们可以对集合进行成员资格检查:

{% for emp in emps %}
<td>
{% if  emp.id not in empprocess %}
<a href="/view_client_process/{{ emp.id }}"><button class="btn btn-info">Edit</button></a>
{% else %}
<a href="/view_client_process/{{ emp.id }}"><button class="btn btn-info">Assign</button></a>
{% endif %}
</td>
{% endfor %}

注意:您可能希望将字段username重命名为user,因为对用户的ForeignKey与用户名不同

 

注意:请使用{% url ... %}模板标签 [Django-doc] 而不是自己执行 URL 处理。

最新更新