In this post, we are going to see what happens behind the scenes when a file is uploaded to a django powered web application.
An HTML form with a file input (atleast one) and encoding set to
is submitted. The
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
objects (through the
request.FILES values are a sequence of instances of
In the django form, we pass the
request.FILES MultiValueDict. These
UploadedFile instances are validated by the
full_clean method on the
method in turn calls the
method which calls the
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,
save_base method calls a
pre_save method on each field of the
method returns the value of the file instance bound to that FileField
and calls it's
save method. This
method (on the models.FileField) in turn calls the save
method on the
Storage which is either picked up from the arguments
passed to the FileField (
FileField(storage=...)) or the
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 writen 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 developmenrt help, contact us today ✉.
Would you like to download 10+ free Django and Python books? Get them here