I'm working on a user registration form. In this form there is an ImageField field for the user to upload his photo there. The point is that this ImageField only exists in forms.py, in models. py the photo is saved in a BinaryField called photo.
In my views.py when saving the form I transform this image into base64, save the image in the photo field and save the form.
So far so good, I can save the form. The problem is to edit this user form, how do I upload this photo for the user to view it in the template?
models.py:
class User(models.Model):
photo = models.BinaryField(null=True, blank=True)
forms.py:
class UserForm(forms.ModelForm):
image = forms.ImageField(required=False, label='Photo')
views.py:
if form.is_valid():
if form.cleaned_data['image']:
user = form.save(commit=False)
user.photo = base64.encodestring(form.cleaned_data['image'].file.read())
user.save()