models.py:
class Empresa(models.Model):
logo = models.FileField(null=True)
forms.py:
class ConfiEmpresa(ModelForm):
logo = forms.FileField(required=False)
class Meta:
model = Empresa
The html input code that the form field is rendering is the following one:
<input type="file" name="logo" id="id_logo">
views.py:
import base64
def configempresa(request):
if request.method == "POST":
form = ConfiEmpresa(request.POST, request.FILES)
print(form.errors) # I'm not having any form error here
if form.is_valid():
logo = form.cleaned_data.get('logo')
print(logo) # It's printing "None"
logo = base64.encodebytes(logo)
#...rest of the view
The error I'm getting:
TypeError at /Config/empresa/
expected bytes-like object, not NoneType
So in conclusion I'm trying to convert the jpg file that user is setting as 'logo' to base64 in order to store it in my DB and later decode it to get the image where I need it.
Of course I'm doing something wrong, I think it's in my view. How can I handle the value of the form field logo
in order to convert it to base64?
I'm not storing the raw file in any folder.