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

179
Visualizações
python filter none values

I have:

1. models.py

class Post(models.Model):
    ROOT_CHOICES = (
        ('Manufacturing', 'Manufacturing'),
        ('Transportation', 'Transportation'),
        ('Installation', 'Installation'),
        ('Operation', 'Operation'),
    )
    root1 = models.CharField(max_length=250, choices=ROOT_CHOICES, default='Manufacturing')
    root2 = models.CharField(max_length=250, choices=ROOT_CHOICES, null=True, blank=True)
    root3 = models.CharField(max_length=250, choices=ROOT_CHOICES, null=True, blank=True)
    root4 = models.CharField(max_length=250, choices=ROOT_CHOICES, null=True, blank=True)

2. views.py

def post_detail(request, slug):
    post=get_object_or_404(Post, slug=slug)
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post=post
            comment.save()
            return redirect('data:post_detail', slug=post.slug)
    else:
        form=CommentForm()
    template = 'data/post_detail.html'
    context = {'form':form,'post':post}
    return render(request, template, context)

3. html

<div class="col-sm-3">
    <div class="post-parameters">
         <p class="text-parameters"> Root: {{ post.root1 }}, {{ post.root2 }}, {{ post.root3 }}, {{ post.root4 }}</p>
    </div>
 </div>

It looks like this, right now, in my html file: Root:Manufacturing, None, None, None which is correct. However I would like not to show the ,if any, None values e.g. Root:Manufacturing

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can use the default_if_none filter, so: {{post.root1|default_if_none:""}}

Alternatively, you can check for each value specifically

{% if post.root1 %} {{post.root1}} {% endif %}
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