How to Get the Size of an Uploaded File with Python in Django 


               


How to Get the Size of an Uploaded File with Python in Django



Python


In this article, we show how to get the size of an uploaded file with Python in Django.

So, imagine on your website, you have a file uploader form, where users can upload images, . videos, etc.

Normally, you wouldn't want to display the size of the file that the user uploaded, but in certain instances, you may want to.

If you have a site where you want the user to see the size of file uploads, such as in the file manager area or whatever, then we'll show how to do this in this article.

So let's say we create a basic database table, File, in the models.py file, shown below.



So we create a database table called File.

We make it very simple.

We simply have name and filepath columns.

name represents the name of the file, such as the title of the file.

filepath represents the pathway to the file.

Now we go to the views.py file.

In this views.py file, we obtain the last object in the database table and create a variable that stores the size of this file object.

The following, shown below, is the code of the views.py file.



So we have the contents of our views.py file shown above.

We import render, since we're rendering a template file.

We import the database table (model) File, that we created in the models.py file.

And we have to import os, since we we use this module to get the size of the file.

So, in this example, we create the variable, lastfile, and set it equal to, File.objects.last()

So the variable, lastfile, contains the last object in the File database table.

We then create the variable, file, and set it equal to, lastfile.filepath

This gets the filepath of the last object in the File database.

We then create a variable, fullfilepath. Since all files that we upload are upload to media/files/ directory, we must specify the full path to the file.

We convert the variable file to a string, because it's the data from a FileField. In order to use functions on the data, it must be of type String, not FileField.

We then create a variable called filesize and set it equal to, os.path.getsize(fullfilepath).

This gets the size of the file, specified in the file path.

We then pass in the variables file and filesize to the context dictionary.

We then render the context dictionary to the home.html file in the Blog directory.

Then in the home directory, you can simply display the filesize through use of the line, {{ filesize }}

In case you're wondering, getting files through the dictionary, request.FILES[] wouldn't work, because the data in request.FILES[] is temporary. The solution we showed in this article allows for the permanent display of the file size.

And this is all that is required to get the size of an uploaded file with Python in Django.


Related Resources

How to Create a Video Uploader with Python in Django

How to Create an Image Uploader with Python in Django



HTML Comment Box is loading comments...