How to handle images in base64 in Django?

In Updateview to properly handle the file coming from the request.POST['photos'] to base64 format?
July 2nd 19 at 17:20
2 answers
July 2nd 19 at 17:22
Solution
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())
July 2nd 19 at 17:24
If you want to save these images in the field model, this is done as follows:

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()

Find more questions by tags Django