mridul
mridul

Reputation: 2096

Django : Read multiple file from html form

I am trying to upload multiple files using Django. Using following code select multiple files in html form. index.html

IMAGE Files:<input type="file" name="image" multiple /><br/>

Views.py

image=request.FILES.get('image')

models.py

image=models.ImageField(upload_to=get_upload_pathimage)

Now I get only last file (if I select 3 files then get 3rd file). How to get all images ?

Upvotes: 1

Views: 1190

Answers (3)

Jhanvi Patel
Jhanvi Patel

Reputation: 239

My solution for getting multiple files from html and upload that files in django

index.html

<div id="fine-uploader-manual-trigger">
<input class="custom-file" type="file" name="imgname" id="productImgUpload" accept=".xlsx,.xls,image/*,.doc,audio/*,.docx,video/*,.ppt,.pptx,.txt,.pdf" multiple>
</div> 

views.py

filelist = request.FILES.getlist('imgname')
for i in range(len(filepath)):
      filename = filelist[i]

Upvotes: 2

Hari Suryawanshi
Hari Suryawanshi

Reputation: 1

i just try for getting multiple images from static folder to html page using for loop in Django.

{% for i in lst %}

   <td style="margin-left: 10px;">
   <img src="{% static '/images2/' %}{{i}}.png" style="width: 300px; height: 200px;">
{% endfor %}

Upvotes: -1

Mark Lavin
Mark Lavin

Reputation: 25164

request.FILES is a MultiValueDict and doing get will return only the last value as you noted. If you want all of the values you should use images = request.FILES.getlist('image').

Upvotes: 1

Related Questions