Александр
Александр

Reputation: 289

Get items from request.FILES

I tried to get items by request.FILES: from input <input type='file' name='images' multiple> I recieve to django views the data:

<MultiValueDict: {
u'images': [
<InMemoryUploadedFile: Men_Mother_Boys_Little_girls_Smi.jpg (image/jpeg)>,
<InMemoryUploadedFile: tumblr_p4fq65rkPh1uc0ho8o1_1280.jpg (image/jpeg)>, 
<InMemoryUploadedFile: tumblr_nxq18wYT4l1uc0ho8o1_500.jpg (image/jpeg)>, 
<InMemoryUploadedFile: ddc4790b00ee3c8d51c106afa76a4dc7.jpg (image/jpeg)>, 
<InMemoryUploadedFile: 9961f0d0ea1b54cdd9d4a2c58a66c6be.jpg (image/jpeg)>
]}>

I try get the list of files:

images = self.request.FILES.get('images', None)

But in images always I receive just single <InMemoryUploadedFile>, although I expect list of InMemoryUploadedFile. I checked:

    print(type(self.request.FILES))  #<MultiValueDict: {u'images': [<InMemoryUploadedFile:
    print(type(images))              #InMemoryUploadedFile

I find this rather atypical for behavior a dictionary. Maybe this is a pre-planned behavior intended to receive for single file. Is that how it should be?

Upvotes: 1

Views: 1203

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476584

As specified in the output, you are working with a MultiValueDict, so you can use the getlist method [GitHub]:

images = self.request.FILES.getlist('images', None)

Upvotes: 3

Related Questions