Friday, September 22, 2017

How to clean a form which receives multiple files

I am not happy with this snippet from the django docs:


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)

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

My problem: The form instance can't validate the files.

AFAIK in the method form.clean() I don't have a list of files.

I can work around this, but it breaks the nice separation of concerns which
I usually get with the nice django form library :-)

Imagine my app receives three files. Is there a way to validate these files inside form.clean()?

Regards,
Thomas Güttler

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/07e6b78b-6af2-466b-acb3-0a285c3e201b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment