0

I've build a form that works just fine for creating new objects. I can also update all the text fine. It does not let me update images. I am using Crispy Forms to build the form. Where did I make a mistake?

My model looks like this:

class Bottle(models.Model):
    class displaySorting(models.IntegerChoices):
        Lighthouse = 1
        Regular = 2

    name = models.CharField(max_length=255)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    sorting = models.IntegerField(choices = displaySorting.choices,verbose_name='Sort order')
    brand = models.ForeignKey(Brand, on_delete=models.CASCADE)
    bottle_size = models.IntegerField(validators=[MaxValueValidator(9999)])
    info = HTMLField()
    tasting_notes = HTMLField()
    abv = models.DecimalField(max_digits=3, decimal_places=1, verbose_name='Alcohol %')
    image = ResizedImageField(size=[1000,1000], upload_to=image_upload_handler)
    thumbnail = models.ImageField(upload_to='thumbnails')
    shop_link = models.URLField()
    consumer_shop_link = models.URLField()
    website_link = models.URLField()

  def save(self, *args, **kwargs):
        self.slug = slugify(f"{self.brand}-{self.name}")
        output_size = (300, 169)
        output_thumb = BytesIO()

        img = Image.open(self.image)
        img_name = self.image.name.split('.')[0]

        if img.height > 300 or img.width > 300:
            img.thumbnail(output_size)
            img.save(output_thumb,format='JPEG',quality=90)

        self.thumbnail = InMemoryUploadedFile(output_thumb, 'ImageField', f"{self.brand}-{img_name}_thumb.jpg", 'image/jpeg', sys.getsizeof(output_thumb), None)

        super(Bottle, self).save()

My view looks like this:

@login_required
def bottle_update_view(request, id=None):
    object = get_object_or_404(Bottle, id=id)
    form = BottleForm(request.POST or None, instance=object)

    context = {
        'form':form,
        'object':object
    }

    if form.is_valid():
        form.save()
        context['message'] = 'Data saved'

    return render(request,'Academy/bottle_crud.html',context)

and finally my template like this:

    {% block content %}

<div class='container'> 
    <div class='row form'>
        <h1 class='text-center my-5'>Add & Update bottle</h1>
       {% if message %}
            <div> 
                <div><h2>{{message}}</h2></div>
                <div><a href="{{object.get_bottle_link}}"><h2 class='link'>Check bottle</h2></a></div>
            </div>
        
       {% endif %}

       <form enctype='multipart/form-data' method='POST'>
        {% crispy form %}
        </form>
    </div>
</div>



{% endblock content %} 

EDIT: I've added the request.FILES to to form like this:

form = BottleForm(request.POST or None, request.FILES, instance=object)

But the the form will no longer load any info to be updated, it's just an empty form now.

0 Answers0