How to clean a form which receives multiple files

60 Aufrufe
Direkt zur ersten ungelesenen Nachricht

guettli

ungelesen,
22.09.2017, 09:18:1722.09.17
an Django users
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

m1chael

ungelesen,
22.09.2017, 12:24:2322.09.17
an django...@googlegroups.com
I do it this way too and agree that it feels dirty. Curious to hear what others have to say about this!

--
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...@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.

guettli

ungelesen,
25.09.2017, 04:19:2425.09.17
an Django users
thank you very much for your Feedback!
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Tim Graham

ungelesen,
25.09.2017, 13:03:3925.09.17
an Django users
There's a pull request to allow FileField(multiple=True): https://github.com/django/django/pull/9011
Allen antworten
Antwort an Autor
Weiterleiten
0 neue Nachrichten