From HTML Form to Storage

In this post, we are going to see what happens behind the scenes when a file is uploaded to a django powered web application.

File Upload Flow{.align-center width=”543px” height=”792px”}

An HTML form with a file input (atleast one) and encoding set to multipart/form-data is submitted. The MultiPartParser parses the POST request and returns a tuple of the POST and FILES data (request.POST, request.FILES). The MultiPartParser processes the uploaded data using the File Upload Handlers objects (through the new_file, receive_data_chunk and upload_complete methods). The request.FILES values are a sequence of instances of UploadedFile.

In the django form, we pass the request.FILES MultiValueDict. These UploadedFile instances are validated by the full_clean method on the Form. The full_clean method in turn calls the _clean_fields method which calls the clean method on the forms.FileField and checks if the data is empty.

After the form is successfully validated, we might assign and save the cleaned_data of the form to a model instance (or by saving the ModelForm). When the save method on the model instance is called, the save_base method calls a pre_save method on each field of the model. This pre_save method returns the value of the file instance bound to that FileField and calls it's save method. This save method (on the models.FileField) in turn calls the save (Storage) method on the Storage which is either picked up from the arguments passed to the FileField (FileField(storage=...)) or the DEFAULT_FILE_STORAGE settings attribute.

This is the flow from the HTML Form all the way upto the File Storage. Hope you liked it.

Thank you for reading the Agiliq blog. This article was written by thejaswi on Sep 21, 2011 in upload .

You can subscribe ⚛ to our blog.

We love building amazing apps for web and mobile for our clients. If you are looking for development help, contact us today ✉.

Would you like to download 10+ free Django and Python books? Get them here