What is the best way to allow a user to do an arbitrary number of file uploads with Django?
If I knew I was going to allow the user to do up to two file uploads then I could just do this in my forms.py
...
from django import forms
class UploaderForm(forms.Form):
file1 = forms.FileField(upload_to='user_files')
file2 = forms.FileField(upload_to='user_files')
...and this in my views.py
...
...
if request.method == 'POST':
form = UploaderForm(request.POST)
if form.is_valid():
form.save()
# do something.
else:
form = UploaderForm()
...
However, I'm planning on using Javascript on the frontend to allow the user to continually add new <input type="file" name="fileN" />
elements (where N is the Nth file upload field added to the page.) The user can choose to do their upload with one or more files selected.
Is there any way that I can use Django's built in forms to accomplish this?
If, as I suspect, I can't utilize Django forms to generate the forms, can I use Django's validation? i.e., it would be handy to be able to access form.cleaned_data['file0']
, form.cleaned_data['file1']
, etc., instead of having to do my own validation.
Thanks.