Home > Software design >  How can I Display Link in List when User is found in another Django Model
How can I Display Link in List when User is found in another Django Model

Time:08-01

I have a search box with a list of users with a hyperlink for approving applications. I want to show Approved on the link on any user whose application is already approved and Approve link on not approved application. I have tried putting the hyperlink in a for loop with a conditional statement to do the check but the Approve button is displaying twice on those users whose application not Approved while on the those Approved Application, the Approve and Approved links are displayed. someone should gracefully help as I am Django beginner so I finding it difficult to go from here. Any better way of achieving the same thing would be much appreciated. Thanks

Models code:

class Fee(models.Model):
    applicant = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
    email = models.CharField(max_length=30, null=True)
    phone = models.CharField(max_length=20, null=True)
    date = models.DateTimeField(auto_now_add=True)


def __str__(self):
    return f'Payments: {self.applicant}'

class Profile(models.Model):
    applicant = models.OneToOneField(User, on_delete=models.CASCADE, null = True)
    surname = models.CharField(max_length=20, null=True)
    othernames = models.CharField(max_length=40, null=True)
    gender = models.CharField(max_length=6, choices=GENDER, blank=True, null=True)

    def __str__(self):
        return f'{self.applicant.username}-Profile'

views code:

def search_applicants(request):


    #Set an Empty Dict of Context
    context = {}
    #Search Applicant Form
    searchForm = SearchApplicantForm(request.GET or None)
    payments = Fee.objects.all()

    if searchForm.is_valid():
        #Value of search form
        value = searchForm.cleaned_data['value']
        #Filter Applicant by Surname or Othernames using Q Objects
        user_filter = Q(surname__icontains = value) | Q(othernames__icontains = value)
        #Apply the Profile Object Filter
        list_submited = Profile.objects.filter(user_filter) 

    else:
        list_submited = Profile.objects.all()

    paginator = Paginator(list_submited, 5)
    page = request.GET.get('page')
    paged_list_submited = paginator.get_page(page)
    #Update context variable
    context.update({
    'list_applicants':paged_list_submited,
    'searchForm':searchForm,
    'payments':payments,

    })

    return render(request, 'user/list_applicants.html', context)

HTML Template code:

<table >

<thead >

<tr>

<th scope="col">#</th>

<th scope="col">Surname</th>

<th scope="col">Othernames</th>

<th scope="col">Email</th>

<th scope="col">Phone Number</th>

<th scope="col">Select</th>

</tr> 


</thead>
                    

{% if list_applicants %}

<tbody>

{% for applicants in list_applicants %}

<tr>
                        
<td>{{ forloop.counter }}</td>
                        

<td>{{ applicants.applicant.profile.surname }}</td>
                        

<td>{{ applicants.othernames }}</td>
                        

<td>{{ applicants.applicant.email }}</td>
                        
<td>{{ applicants.applicant.profile.phone }}</td>

<th >
{% for fee in payments %}

{% if fee.applicant == applicants.applicant %}

<a >Approved</a>

{% else %}

<a  href="{% url 'approve-applicant' applicants.id %}">Approve</a>

{% endif %}

{% endfor %}

</th>
                      
</tr>
                      
{% endfor %}
      
</tbody>
                    
{% else %}                 

No Records Found
{% endif %}

</table>

CodePudding user response:

You are using OneToOne field so you don't even need

  payments = Fee.objects.all()

in your context.

All you need to is check in template that this user have Fee object:

<th>


{% if  applicants.applicant.fee %}

<a >Approved</a>

{% else %}

<a  href="{% url 'approve-applicant' applicants.id %}">Approve</a>

{% endif %}


</th>
  • Related