Behind the Scenes: From HTML Form to Storage

By : Thejaswi Puthraya

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

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.


Related Posts


Can we help you build amazing apps? Contact us today.

Topics : django files upload

Comments

Software Outsourcing Company

Loving the information on this website , you have done outstanding job on the posts . thanks..

commmenttor
Yagnaum

Build everything from personal blogs to enterprise applications at Yagnaum's Drupal development center @ http://www.yagnaum.com/expertise_drupal.php

commmenttor
What Nike Free Running Shoes Provide You

Behind the Scenes: From HTML Form to Storage - Agiliq Blog | Django web app development

commmenttor
Post a comment Name :

Email :

Your site url:

Comment :

© Agiliq, 2009-2012