Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

114
Visualizações
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 Respostas
Responde à pergunta

0

{% if not purpose_is_not_exist %}

You should negate the False bool.

over 4 years ago · Santiago Trujillo Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda