[Django]-Why does this Django form complain "this field is required" for an image field?

6👍

You have to pass an image not in data, but in the files parameter of the form.

from django.core.files.uploadedfile import SimpleUploadedFile

img = open('photocompetitions/static/img/body_bg.jpg')
uploaded = SimpleUploadedFile(img.name, img.read())
photoform = PhotoForm(data_photo, files={'image': uploaded})

2👍

You need to use ‘rb’ options for open file:

'image': open('photocompetitions/static/img/body_bg.jpg', 'rb')

0👍

Did you try to use a bmp rather than a jpg?

I remember that there is an issue with the way PIL is loaded by the django test framework.

the jpg format may not be enabled correctly in this case but bmp should be ok.

I hope it helps

Leave a comment