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

231
Vistas
Django/Pillow - Image resize only if image is uploaded

I'm able to upload an image and resize it, but if I submit the form without images I get this error

The 'report_image' attribute has no file associated with it.

What should I do if no image is uploaded?

This is my models.py

class Report(models.Model):

    options = (
        ('active', 'Active'),
        ('archived', 'Archived'),
    )

    category = models.ForeignKey(Category, on_delete=models.PROTECT)
    description = models.TextField()
    address = models.CharField(max_length=500)
    reporter_first_name = models.CharField(max_length=250)
    reporter_last_name = models.CharField(max_length=250)
    reporter_email = models.CharField(max_length=250)
    reporter_phone = models.CharField(max_length=250)
    report_image = models.ImageField(_("Image"), upload_to=upload_to, null=True, blank=True)
    date = models.DateTimeField(default=timezone.now)
    state = models.CharField(max_length=10, choices=options, default='active')

    class Meta:
        ordering = ('-date',)

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)
        img = Image.open(self.report_image.path)

        if img.height > 1080 or img.width > 1920:
            new_height = 720
            new_width = int(new_height / img.height * img.width)
            img = img.resize((new_width, new_height))
            img.save(self.report_image.path)


    def __str__(self):
        return self.description
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I found the solution. Needed to add this check before the actual resize.

if self.report_image:

This way, if no image has been uploaded it will just ignore the resize and proceed without it.

This is the new relevant part:

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)

        if self.report_image: #check if image exists before resize
            img = Image.open(self.report_image.path)

            if img.height > 1080 or img.width > 1920:
                new_height = 720
                new_width = int(new_height / img.height * img.width)
                img = img.resize((new_width, new_height))
                img.save(self.report_image.path)
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