0

I have created a form to upload files based on this guide:

Need a minimal Django file upload example

I want to adapt this to allow for multiple file uploads. I've tried a couple of approaches without much luck. Any advice appreciated.

Upload form

        <form action="{% url 'list' %}" method="post" enctype="multipart/form-data">
        {% csrf_token %}
        <p>{{ form.non_field_errors }}</p>
        <p>{{ form.docfile.label_tag }} {{ form.docfile.help_text }}</p>
        <p>
            {{ form.docfile.errors }}
            {{ form.docfile }}
        </p>
        <p><input type="submit" value="Upload" /></p>
    </form>

views.py

def list(request):
if request.method == 'POST':
    form = DocumentForm(request.POST, request.FILES)
    if form.is_valid():
        newdoc = Document(docfile=request.FILES['docfile'])
        newdoc.save()

        return HttpResponseRedirect(reverse('list'))
else:
    form = DocumentForm()  # A empty, unbound form

documents = Document.objects.all()

return render(
    request,
    'check/list.html',
    {'documents': documents, 'form': form}
)

forms.py

class DocumentForm(forms.Form):
docfile = forms.FileField(
    label='Select a file',
    help_text='max. 42 megabytes',
)
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
Aaron
  • 3
  • 2

3 Answers3

1

You can add the 'multiple' attribute to a file input in your form. You control or shift select the files you want.

<input type="file" name="files" multiple />

Then in your view you can use:

if request.method == 'POST':
    for file in request.FILES.getlist('files'):
        # do something with each file
chaggy
  • 1,091
  • 1
  • 10
  • 18
0

I assume you want multiple upload widgets to appear on one page. Without additional add-ons, the user won't just be able to select multiple images and upload them all at the same time. If you want the user to be able to upload 5 images on the same page, you'll have to have 5 image upload fields or widgets. Currently, your form would only have one.

This can be accomplished using formsets. Essentially, what a formset is is something that allows you to display multiple instances of the same form on the same page.

Read up on formsets and let us know if you have any more questions about this. I've implemented exactly what you're wanting to do on my site with basic formset functionality and it works good.

0

You can do something like

forms.py

from django import forms

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

views.py

from django.views.generic.edit import FormView
from .forms import FileFieldForm

class FileFieldView(FormView):
    form_class = FileFieldForm
    template_name = 'upload.html'  # Replace with your template.
    success_url = '...'  # Replace with your URL or reverse().

    def post(self, request, *args, **kwargs):
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        files = request.FILES.getlist('file_field')
        if form.is_valid():
            for f in files:
                ...  # Do something with each file.
            return self.form_valid(form)
        else:
            return self.form_invalid(form)


Reference: https://docs.djangoproject.com/en/2.0/topics/http/file-uploads/#uploading-multiple-files

anjaneyulubatta505
  • 10,713
  • 1
  • 52
  • 62
  • Thank you. That seems to be working. I seem to have the files in 'file'. Now I just have to work out how to write them to disk. Thank you. – Aaron May 18 '18 at 17:40