B
B
blackbb2016-11-13 03:17:02
Django
blackbb, 2016-11-13 03:17:02

How to process images in base64 in Django?

How to correctly process files coming from request.POST['photos'] in base64 format in Updateview?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
blackbb, 2016-11-15
@blackbb

def form_valid(self, form):
        photos = self.request.POST.getlist('photos[]')
        for strg in photos:
            strg =strg.partition('base64,')[2]
            img_data = base64_decode(strg)
            photo = CompanyImage(order=self.object)
            photo.file_up.save('123.jpg', ContentFile(img_data))
            photo.save()
        return HttpResponseRedirect(self.get_success_url())

I
Igor Shevchenko, 2016-11-13
@igor_shevchenko

If you want to store these images in the model field, then it is done like this :

from base64 import b64decode
from django.core.files.base import ContentFile

image_data = b64decode(b64_text)
my_model_instance.cool_image_field = ContentFile(image_data, 'whatup.png')
my_model_instance.save()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question