【问题标题】:if same id is present in two tables show edit button else show assign button in django如果两个表中存在相同的 id 则显示编辑按钮,否则在 django 中显示分配按钮
【发布时间】:2020-01-02 14:14:44
【问题描述】:

获取所有员工个人资料表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 %}

【问题讨论】:

    标签: django-models django-templates django-views


    【解决方案1】:

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

    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 ... %} template tags [Django-doc],不要自行处理网址。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-29
      • 1970-01-01
      • 2019-12-15
      • 2018-03-16
      • 1970-01-01
      • 1970-01-01
      • 2020-11-13
      • 1970-01-01
      相关资源
      最近更新 更多