S
S
SHADRIN2020-06-27 21:11:50
Django
SHADRIN, 2020-06-27 21:11:50

Why is the list of files empty Django?

I'm trying to save uploaded files, but the list of files is empty, what's wrong?

upload

<form enctype="multipart/form-data" method="post">
    {% csrf_token %}
    {{ form.attachments }}
    <button type="submit">Upload</button>
</form>


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

    def post(self, request, *args, **kwargs):
        print('Post')
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        files = request.FILES.getlist('file_field')
        print(files)
        if form.is_valid():
            for f in files:
                print('Save')
                myfile = f
                filesystem = FileSystemStorage()
                filename = filesystem.save(myfile.name, myfile)

                uploaded_file_url = fs.url(filename)
            return self.form_valid(form)
        else:
            return self.form_invalid(form)


forms
from django import forms

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


Tell me where is the mistake? Printing files outputs [] an empty list

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SHADRIN, 2020-06-27
@shadrin_ss

The issue is resolved instead of file_field specified atachments and the list works.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question