Answer the question
In order to leave comments, you need to log in
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>
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)
from django import forms
class FileFieldForm(forms.Form):
attachments = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question