I am trying to allow my users to select multiple files when uploading file(s). I am trying to allow the field on the forms.py file to have this functionality with the "multiple" property. I have seen other examples found online, but it does not work for me.
forms.py", line 168, in Meta
"image": ClearableFileInput(attrs={"multiple": True}),
File "/x/x/x/lib64/python3.6/site-packages/django/forms/widgets.py", line 393, in __init__
% self.__class__.__qualname__
ValueError: ClearableFileInput doesn't support uploading multiple files.
pip list
Package Version
----------------- -------
asgiref 3.4.1
Django 3.2.21
django-auth-ldap 4.0.0
django-filter 21.1
lxml 4.9.3
mysqlclient 2.1.1
Pillow 8.4.0
pip 21.3.1
pyasn1 0.5.0
pyasn1-modules 0.3.0
python-docx 0.8.11
python-ldap 3.4.3
pytz 2023.3
setuptools 39.2.0
sqlparse 0.4.4
typing_extensions 4.1.1
forms.py
from django.forms import ModelForm, Textarea, Select, ClearableFileInput
class UploadImageForm(ModelForm):
class Meta:
model = Images
fields = [
"image",
"description"
]
labels = {
"image": ("Image"),
"description": ("Description")
}
widgets = {
"image": ClearableFileInput(attrs={"multiple": True}),
}
models.py
class Images(models.Model):
id = models.BigAutoField(primary_key=True)
image = models.ImageField(upload_to='images/', null=True)
test_part_id = models.ForeignKey('TestParts', on_delete=models.PROTECT, blank=True, null=True)
description = models.CharField(max_length=512, blank=True)
class Meta:
db_table = 'images'
def __str__(self):
return f"{self.image}"
views.py
def testing(request):
if request.method == "POST" and "fileForm" in request.POST:
imageForm = UploadImageForm(request.POST, request.FILES)
if imageForm.is_valid():
for image in request.FILES.getlist('files'):
new_image = Images(image=image)
new_image.save()
return redirect(fa_details, id=id)
I've included the view, but I don't think there is anything wrong in it that would prevent me from handling multiple files. I manually created a form in the HTML/template and I am able to process multiple images and save into the database with no problems. I could go this route, but something tells me it is not the recommended route.
I have even tried the official Django documentation, overriding the field in the form class instead of using the meta class(below). It throws the same error.
file_field = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))