Skip to content

Instantly share code, notes, and snippets.

@rg3915
Created May 4, 2023 18:23
Show Gist options
  • Select an option

  • Save rg3915/a5a26498056e15d9f7bf15dd9891629e to your computer and use it in GitHub Desktop.

Select an option

Save rg3915/a5a26498056e15d9f7bf15dd9891629e to your computer and use it in GitHub Desktop.
bugfix: ValueError: ClearableFileInput doesn't support uploading multiple files.

Dica: bugfix quem estiver mexendo com upload múltiplo vai se deparar com este erro:

ValueError: ClearableFileInput doesn't support uploading multiple files.

Para corrigir faça em forms.py

from django import forms


class MultipleFileInput(forms.ClearableFileInput):
    allow_multiple_selected = True


class MultipleFileField(forms.FileField):
    def __init__(self, *args, **kwargs):
        kwargs.setdefault("widget", MultipleFileInput())
        super().__init__(*args, **kwargs)

    def clean(self, data, initial=None):
        single_file_clean = super().clean
        if isinstance(data, (list, tuple)):
            result = [single_file_clean(d, initial) for d in data]
        else:
            result = single_file_clean(data, initial)
        return result


class FotoForm(forms.ModelForm):
    foto = MultipleFileField()

    class Meta:
        model = Foto
        fields = ('foto',)

Fixed in Django 3.2.19, 4.1.9 e 4.2.1

https://docs.djangoproject.com/en/4.2/releases/4.2.1/#cve-2023-31047-potential-bypass-of-validation-when-uploading-multiple-files-using-one-form-field

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment