Reputation: 177
That is in templates, I have the following code. the file input has a multiple attribute in the end so that I can select multiple images.
<form method='POST' action="{% url 'method' %}" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="img" multiple />
<input type="submit" />
</form>
<br />
Then I am having trouble to save the image to my model. I tried to print request.FILES, and I got this
<MultiValueDict: {'img': [<InMemoryUploadedFile: 1.jpg (image/jpeg)>,
<InMemoryUploadedFile: 2.jpg (image/jpeg)>, <InMemoryUploadedFile: 3.jpg
(image/jpeg)>]}>
There are 3 items in 'img', I also used request.FILES.getlist('img'), doesn't really work out. How am I suppose to access the content in the 'img' like accessing several individual request.FILES. Thanks!!!
Upvotes: 1
Views: 437
Reputation: 177
Well, I cannot believe I solved the problem 10mins after I posted it. The key is still request.FILES.getlist('img'), but make sure you don't use chunks() later, so if you want save the image, save it directly like
for image in request.FILES.getlist('img'):
xxxx.image = image
xxx.save()
That's it. don't use any chunks() methods.
Upvotes: 1