I have a form that has an image select input and a text input, the latter expects a URL with an image.
I would like to upload both the image from the ImageField and the image from the URL.
I have got as far as completing various checks on the URL, and initialising a variable imgfile
with the file data that has the type _io.BytesIO
, and imgname
that has the type str
.
When I test what is in request.FILES (from the form’s ImageField) I see this:
print(type(self.request.FILES)
-> <class 'django.utils.datastructures.MultiValueDict'>
print(dict(self.request.FILES)
-> {'image_set-0-file': [<InMemoryUploadedFile: icons0.1.gif (image/gif)>]}
Then I add an element to self.request.FILES
:
self.request.FILES['imgurl'] = InMemoryUploadedFile
print(dict(self.request.FILES)
-> {'image_set-0-file': [<InMemoryUploadedFile: icons0.1.gif (image/gif)>], 'imgurl': [<class 'django.core.files.uploadedfile.InMemoryUploadedFile'>]}
So, at this point I am thinking that the imgurl
dict element is not right, but I soldier on:
self.request.FILES['imgurl'].name = 'hello.png'
print(dict(self.request.FILES)
-> {'image_set-0-file': [<InMemoryUploadedFile: hello.png (image/gif)>], 'imgurl': [<class 'django.core.files.uploadedfile.InMemoryUploadedFile'>]}
Now it’s obvious that this is not working, because self.request.FILES['image_set-0-file'].name
has changed rather than self.request.FILES['imgurl'].name
.
Is this just fundamentally the wrong technique, or have I made a minor (or major) coding error?
Thanks