Opened 8 weeks ago

Last modified 8 weeks ago

#31710 new Cleanup/optimization

Added all files validation to the "Uploading multiple files" example.

Reported by: nawaik Owned by: nobody
Component: Documentation Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by nawaik)

When following the multiple files documentation:

If you change:

file_field = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))


file_field = forms.ImageField(widget=forms.ClearableFileInput(attrs={'multiple': True}))

The file input field looks like this:

<input type="file" name="file_field" multiple="" accept="image/*" required="" id="id_file_field">

If you do inspect element and remove accept="image/*" so it looks like this:

<input type="file" name="file_field" multiple="" required="" id="id_file_field">

If you then only add 1 .txt file everything works fine and you get an error:

"Upload a valid image. The file you uploaded was either not an image or a corrupted image."

However if you upload multiple .txt files or a mix of images and files nothing gets checked and you are able to upload .txt files in imagefields.

I tried this with a fresh project and was able to reproduce the problem.

This is how it looks in the sqlite db:

id	image	                        post_id
4	products/testnow_copy_2.txt	2

Code I used to recreate the problem:

from django.db import models

class Product(models.Model):
    title = models.CharField(max_length=50)

class Images(models.Model):
    post = models.ForeignKey(Product, on_delete=models.CASCADE)
    image = models.ImageField(upload_to='products/')

    def __str__(self):

from django import forms

from .models import Product

class MultiPhotoForm(forms.ModelForm):
    file_field = forms.ImageField(widget=forms.ClearableFileInput(attrs={'multiple': True}))

    class Meta:
        model = Product
        fields = ['title']

from django.shortcuts import render

from .forms import MultiPhotoForm
from .models import Images, Product

def MultiUploadView(request):
    template_name = 'test.html'

    if request.method == "POST":
        form = MultiPhotoForm(request.POST or None, request.FILES or None)
        files = request.FILES.getlist('file_field')
        if form.is_valid():
            title = form.cleaned_data['title']
            product = Product.objects.create(title = title)
            for i in files:
        form = MultiPhotoForm()
    return render(request, template_name, {'form': form})

Change History (3)

comment:1 Changed 8 weeks ago by nawaik

Description: modified (diff)
Version: 3.0master

comment:2 Changed 8 weeks ago by nawaik

Type: UncategorizedBug

comment:3 Changed 8 weeks ago by felixxm

Component: File uploads/storageDocumentation
Summary: Multi upload with Imagefield allows non image files to be uploadedAdded all files validation to the "Uploading multiple files" example.
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

Uploading multiple files contains only a simple example how you can handle multiple files, it will validate only the first file. It's not a bug in ImageField because it doesn't support uploading multiple files.

I agree that we we could improve this example ( with adding all files validation.

Note: See TracTickets for help on using tickets.
Back to Top