0

I put two files in web, but I can only get one file always.

in web

in form.py

    class UFileForm(forms.Form):
        file = forms.FileField(label="资料文件上传", widget=forms.ClearableFileInput(attrs={'multiple': True, 'class': 'bg-info'}))

in view.py

class UploadFileView(View):
    def post(self, request):
        data = {}
        form = UFileForm(request.POST, request.FILES)
        files = request.FILES.getlist('file')
        # print files in request here
        print(files)

result is:

[<InMemoryUploadedFile: 20220609奥莉公会-积分统计表.xlsx (application/vnd.openxmlformats-officedocument.spreadsheetml.sheet)>]

why there is only one file?

1 Answer 1

1

Please see https://stackoverflow.com/a/46409022/14338747 for help using ClearableFileInput functionality with multiple file uploads.

Also, from the Docs. Your views.py must be updated to:

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

class FileFieldFormView(FormView):
    form_class = UFileForm
    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')
        if form.is_valid():
            for f in files:
                ...  # Do something with each file.
            return self.form_valid(form)
        else:
            return self.form_invalid(form)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.