Why django-stdimage not rendering in my html template?

Why image is not rendering in my html template? here is my code:


from django.db import models
from PIL import Image
from stdimage import StdImageField

class Contact(models.Model):
        name = models.CharField(max_length=200)
        image = StdImageField(upload_to="contact/images/",variations={'thumbnail':(640,480)})

#settings.py added ‘stdimage’ in my apps

Suppose I am uploading an image size 1800px by 1000px from my admin panel and it’s giving me the same output after finishing the upload. Why image size not reducing?

The variations add to the image being uploaded, not replace. See the 4th bullet point at the top of the docs.)

Adding that variation means you now have two copies of the image, the original and the resized version.

Further down in the docs it shows how you specify the variation in your templates for rendering. If you want the resized image in your admin view, you’re going to need to replace the rendering of that field with one that will display the variation(s).

Thanks . Yes exactly it making copies of resize image. is there any way to completely replace the uploaded main image with newly resize image in folder where it’s storing the images.

Yes, but probably not using that library. You can handle the upload field yourself, do whatever postprocessing is necessary, and then save the file in its final destination.

I’d suggest that if you’re using a model form, you don’t use the model’s file field in the form. Use a different field in the form for the actual upload, and use the model field after the resize has been performed.

1 Like