Django form has file field and clean_file method.
class UploadFileForm(forms.Form):
file = forms.FileField()
def clean_file(self):
...
And there is a button called 'Apply'
I want to validate the file when I select it, and if the validating is successful, I will click the Apply button which will send a post request with this file.
But I don't understand how I can validate file before submitting it or I should use form into form.
Or it will be in two forms, but then how to throw a file between them
The strategy I've used is to just temporarily save the file so that it persists between requests. Assuming that the validation you mentioned is manual, like previewing the file contents before committing to the database via your "apply" button, then this approach involves two separate requests.
The process looks something like:
initial
attribute/parameter of Django's Form
objects.If your validation process is automated, the stateless nature of HTTP requests/responses is still the key concept. If you don't persist the file outside of memory, the file is essentially gone once the validation fires back the HTTP response to the browser.