79632080

Date: 2025-05-21 13:31:08
Score: 1.5
Natty:
Report link

Depois de um tempo consegui me responder kkk

Garante que teu model de usuário tem isso aqui:

imagem = models.ImageField(upload_to='perfil/', null=True, blank=True)
  1. Confere se o arquivo realmente existe em media/perfil/imagem1.jpg.
    Se não existir, o .url vai quebrar mesmo.

  2. No template, protege assim pra não dar erro:

{% if post.author.imagem and post.author.imagem.name %}
  <img src="{{ post.author.imagem.url }}" alt="{{ post.author.first_name }}"
       class="rounded-circle" style="width: 50px; height: 50px; margin-right: 20px;">
{% endif %}

Aí ele só tenta mostrar a imagem se ela realmente existir de verdade, não só o nome.

Reasons:
  • Blacklisted phrase (1): não
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Leticia Lima