How to Restrict the Size of File Uploads with Python in Django



Python


In this article, we show how to restrict the size of file uploads with Python in Django.

Let's say you're creating a file uploader on your website, maybe for images but you don't want the file size to be above a certain length, let's say 2MB. You want to block all files above 2MB.

This can be done pretty easily in Django.

In this example code we'll go through, we'll block all file uploads above 10MB. So the size of uploads is restricted to or limited to 10MB.

How we do this is just by creating a validator function in the validators.py file.

We then import this validator function into the models.py file and place it in the validators list of the FileField we are validating.

It is pretty straightforward.

So let's first create the database table in the models.py file and then we'll create the custom validator function in the validators.py file.


models.py File

So we're going to create a very basic database table, named File, that has 2 fields, name (name) and filepath (pathway to the file).

This is shown in the code below.



So we create a database table called File.

We make it very simple.

We simply have name and filename columns.

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

filepath represents the pathway to the file.

In this field, filepath, we make the field a FileField. We upload files to the files directory within the media directory. We set the verbose_name equal to "". We set the validators attribute equal to validate_file_size; This the function we're later going to create in the validators.py file.

We then create a __str__ function just so that a generic Object isn't returned when calling a file object.

Just remember that now that you have created the database table in the models.py file, you have to do all migrations, including makemigrations and migrate.

So this is the database table we are working with.

So now we have to create the validator function needed to validate that only PDF (.pdf) and Word (.doc or .docx) files can be uploaded.

Below is the validators.py file.



So at the top of the validators.py file, we must import ValidationError from django.core.exceptions

Inside of this file, we specify the function validate_file_size that passes in the parameter, value.

We then create a variable, filesize, and set it equal to, value.size

size is a built-in attribute of a Django FieldField that allows us to get the size of a file. The size obtained is in bytes.

So we want to limit the file upload size is 10MB.

So we have the do the necessary calculations to find out how many bytes there are in 10MB.

There's 1048576 bytes in 1MB. So in 10 MB, there are 10485760 bytes.

So we write an if statement that if the filesize is greater than 10485760, we raise a validation error that prints out, "The maximum file size that can be uploaded is 10MB"

Else, we return the value.

This is as simple as restricting file size uploads go with Python in Django.

So any files that are less than 10MB will be uploaded. Any file sizes that are greater than 10MB will be blocked from being uploaded.

Below I attempt to upload a file size greater than 10MB and I get the following output.

File size upload restriction in Django



And this is all that is required to restrict or limit file size uploads 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...