Check multiple objects up against request.user in template django

What I basicly want do to is check if an user has applied for one or more of the jobs in a list of jobs.

In my view i have:

jobs = Job.objects.filter(status='open').order_by('-created_at')

And in my template I have:

{% if user == job.creator %}
  {% if not job.get_applicants %}
    <div class = "status wait"><span class = "icon-Coffee" aria-hidden="true"></span><p>Venter</p></div>
  {% elif job.get_numb_applicants == 1 %}
    <div class = "status"><h2>{{job.get_numb_applicants}}</h2><p>Interessert</p></div>
  {% else %}
    <div class = "status"><h2>{{job.get_numb_applicants}}</h2><p>Interesserte</p></div>
  {% endif %}
{% else %}
   <!-- Want to check if user has applied for job here-->
   <div class = "status">
     <span class = "icon-Circle" aria-hidden="true"></span>
     <p>Åpen</p>
   </div>
{% endif %}

This code obviously runs multiple times, one for each job, but since I can't pass a parameter into a function in the template, it is hard to make a comparison between the request.user and each individual job.

I guess the answer lies in the views, but I could not figure out a "clean" way to do it.

The relation between user profiles and jobs is a model called AppliedFor that points to a job and a user profile.

I could not find any good answers to this when I googled it, however if there is, please direct me to the thread!

Upvotes: 0

Views: 81

Answers (1)

aumo
aumo

Reputation: 5554

You can check if the user is contained within the job's applicants.

Assuming job.get_applicants returns a queryset (or any iterable) of User objects, you can use the in operator:

{% if request.user in job.get_applicants %}

Upvotes: 1

Related Questions