Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

115
Vistas
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 Respuestas
Responde la pregunta

0

{% if not purpose_is_not_exist %}

You should negate the False bool.

over 4 years ago · Santiago Trujillo Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda