Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
463 views
in Technique[技术] by (71.8m points)

python - 如果ID在django上的其他表中已经存在,如何禁用或隐藏“分配/重定向”按钮(How to disable or hide assign/redirect button if the ID already exist in other table on django)

class Patient(models.Model):
    name = models.CharField(max_length=50)
    active_choices = [('Yes', 'Yes'),
                      ('No', 'No')]
    active = models.CharField(
        max_length=6, choices=active_choices, default='Yes')

    def __str__(self):
        return self.name

The other one is:

(另一个是:)

class Ticket(models.Model):
    patient = models.ForeignKey(Patient, on_delete=models.CASCADE)   
    is_active = models.IntegerField(default=1)

    def __str__(self):
        return self.patient.name

In the Views.py

(在Views.py中)

@login_required
def PatientView(request):
    form = PatientModelForm(request.POST or None)
    patients = Patient.objects.order_by('-id')
    ticket_list = Ticket.objects.filter(is_active=0)
    total = patient_list.count()       
    if form.is_valid():
        obj.save()
        messages.success(request, 'Patient was added successfully.')
        return redirect('/dashboard/patient')
    context = {
        'form': form,
        'patients ': patients ,    
    }
    return render(request, 'dashboard/patient.html', context)

The other view for ticket:

(票证的另一个视图:)

@login_required
def TicketToGenerateView(request, pk):
    ticket = get_object_or_404(Patient, pk=pk)

    form = TicketModelForm(request.POST or None)
    if form.is_valid():    
        obj.save()
        messages.success(request, 'Patient assigned successfully.')
        return redirect('/dashboard/ticket')

    context = {
        'form': form,
        'ticket': ticket,
    }
    return render(request, 'dashboard/ticket.html', context)

So I want to hide this bellow link button if patient.pk already exist in the Ticket model and is_active = 1 So it displays all rows with assign link button.

(因此,如果故障单模型中已经存在Patient.pk且is_active = 1,那么我想隐藏此波纹管链接按钮,因此它将显示所有带有分配链接按钮的行。)

<a href="{% url 'dashboard:ticket_to' patient.pk %}" name="doctor" class="btn btn-dark btn-sm" data-toggle="tooltip" title="Assign to a Doctor"> <span class=" fa fa-user-md "></span> </a>
  ask by Ahmad Ebrahim translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use a custom template tag

(您可以使用自定义模板标签)

Create a templatetags package under your app directory;

(在您的应用目录下创建一个templatetags ;)

So you should have these inside your app directory:

(因此,您应该在应用程序目录中包含以下内容:)

templatetags
templatetags/__init__.py
templatetags/check_patient.py

Next, add the following to code to <your_app_dir>/templatetags/check_patient :

(接下来,将以下代码添加到<your_app_dir>/templatetags/check_patient :)

from django import template
from <your_app>.models import Ticket

register = template.Library()


@register.simple_tag
def check_patient_already_exists(request, pk):
    return Ticket.objects.filter(patient_pk=pk, is_active=1).exists()

and in your template:

(并在您的模板中:)

{% load check_patient %}

{% if not check_patient_already_exists patient.pk %}

    <a href="{% url 'dashboard:ticket_to' patient.pk %}" name="doctor" class="btn btn-dark btn-sm" data-toggle="tooltip" title="Assign to a Doctor"> <span class=" fa fa-user-md "></span> </a>

{% endif %}

Check the docs for more info

(检查文档以获取更多信息)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...