Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
How in template show button if object is empty?

Can someone say where I did mistake?

I have 2 models: Project and Purpose.

class Purpose(models.Model):
    code = models.UUIDField(_('Code'), primary_key=True, default=uuid.uuid4, editable=False)
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
    text = models.TextField(_('Text'))
    comments = models.ManyToManyField("Comment")

Every project has only one purpose. So in project_detail page I want to show purpose_add button only if that project dont have any other purpose object. Why I dont see button when there is no Purpose object with the same project_code?

views.py:

def project_detail(request, project_code):
 ***
 purpose_is_not_exist = Purpose.objects.exclude(project=project_code).exists()
 ***

project_detail.html:

{% if purpose_is_not_exist %}
   <button id="purpose-add-button"></button>
{% endif %}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

{% if not purpose_is_not_exist %}

You should negate the False bool.

over 4 years ago · Santiago Trujillo Report

0

The confusion is caused by the variable name purpose_is_not_exist.

Purpose.objects.exclude(project=project_code).exists()

Above statement returns whether the Purpose object exists. You shall rename the variable to purpose_exists to avoid any confusion.

And, in the template when you want to add the button if it doesn't exist then negate the variable:

{% if not purpose_exists %}

Alternatively, if you would like to keep using the original name for the variable, then just negate in the view itself.

purpose_is_not_exist = not Purpose.objects.exclude(project=project_code).exists()
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!