Django – How to upload, view, delete file using ModelForm and MySQL


In this tutorial, we’re gonna look at way to upload file in Django using Django ModelForm and save its information to MySQL Database. We also have UI to view list of uploaded items and function to delete specific file.

Related Post: Simple way to upload file in Django

Project Overview

What we will do

We will create a Django Application that allows us to upload files, images and more information corresponding to a data model: Tutorial(title,category,feature_image,attachment).


We can also view the list of all uploaded items and have ability to delete item that we want to remove.


All files (attachments, feature images) will be stored in filesystem, the data model information (title, category, file url) will be saved to MySQL database.


Project Structure

There are several folders and files in our Django project:

django-upload-file-model-form-example-mysql-project-structure contains settings for media path, template & static folder directory. with urlpatterns.
tutorials/ defines functions to handle HTTP request.
tutorials/ defines Tutorial model with FileField and ImageField.
tutorials/ defines ModelForm for Tutorial model.
templates is the template for upload form and list view.

Django Project to upload, view, delete file using ModelForm

Setup Django project
Create project

Create Django project named DjangoUploadModelForm with command:
django-admin startproject DjangoUploadModelForm

Install & Import Python MySQL Client

We have to install Python MySQL Client to work with MySQL database.
In this tutorial, we use pymysql: pip install pymysql.

Once the installation is successful, import this module in DjangoUploadModelForm/

Setup MySQL Database engine

Open DjangoUploadModelForm/ and change declaration of DATABASES:

Create Django App

Run following commands to create new Django App named tutorials:
cd DjangoUploadModelForm
python startapp tutorials

Open tutorials/, we can see TutorialsConfig class (subclass of the django.apps.AppConfig) that represents our Django app and its configuration:

This Django App will be used to upload, view and delete tutorials.

Add Django App to Django project

Open gkzRestApi/, find INSTALLED_APPS, then add:

Configure path for uploaded files

In project’s, set value for MEDIA_ROOT and MEDIA_URL:
+ MEDIA_ROOT: Absolute filesystem path to the directory that holds uploaded files.
+ MEDIA_URL: url for media (uploaded files) from MEDIA_ROOT.

So uploaded files will be accessed with root url /files/..., and they are located at folder named files.

Implement Data Model
Create Data Model

Our Tutorial data model is a Django Model with 4 fields: title, category, feature_image, attachment.

To handle uploaded files, we use model fields: FileField for attachment and ImageField for feature_image.


FileField or ImageField will not help us store file/image in the database but in the filesystem. upload_to parameter specifies where the file/image will be stored. In the code, feature_image will be save to files/tutorial/images folder, and attachment will locate at files/tutorial/attachments. ('files' was indicated by MEDIA_ROOT).

If you want to create more folders for datetime, you can use:
doc = models.FileField(upload_to='docs/%Y/%m/%d/')
The uploaded file will be located at MEDIA_ROOT/files/docs/2019/03/15 for example.

FileField and ImageField are saved as a string field in database that will be references to the real files/images. So we need to override Django Model delete() method, it helps us handle removing actual file/image from storage. If we don’t do this, ONLY Tutorial information (title, category, file info) will be deleted from database, all files still exist.

Run initial migration for data model

Run following Python script:
python makemigrations tutorials

We can see output text:

It indicates that the tutorials/migrations/ file includes code to create Tutorial data model:

The generated code defines a subclass of the django.db.migrations.Migration. It has an operation for creating Tutorial model table. Call to migrations.CreateModel() method will create a table that allows the underlying database to persist the model.

Run the following Python script to apply the generated migration:
python migrate tutorials

The output text:

Check MySQL Database, now we can see that a table for Tutorial model was generated and it’s named tutorials_tutorial:


Create ModelForm for Data Model

Under tutorials app folder, create new file named

This form maps closely to Tutorial model that we’ve created before. fields attribute indicates the order of form fields specified by model field.

For more details about form field that is corresponding of each model field, please visit list of conversions.

Handle upload, view, delete request

Inside tutorials/, define some functions:

tutorialList(): get list of Tutorials from database, render list.html template.

uploadTutorial(): get form data from HTTP POST request, save form data to database and files to filesystem storage, render list.html template if the process is successful. Otherwise, return to upload.html template.

deleteTutorial(): delete a Tutorial by its id (pk).

We can get form data simply just by assigning request.FILES to the file field of TutorialForm. Then, calling will save file object to the 'upload_to' location of FileField/ImageField (defined in Tutorial data model).

Add templates
Create template with form submission

Under project root folder, create templates folder, then create tutorial/upload.html file insides:

The form is submitted via POST method, and the submitted file will be placed in request.FILES.

We must use the attribute enctype="multipart/form-data" in HTML form. If not, request.FILES in views.upload(request) will be empty.

Create template for displaying list

Under templates folder, create tutorial/list.html file insides:

The template gets tutorials data from views and shows it in the table.
We also use a submit button inside a form with POST method to make a delete request to views.

Now we need place for css file.

Create static folder

Under project root folder, create static folder, then create css/tutorial.css file insides:

Specify template & static directory

In project’s, set templates path for 'DIRS':

Then add value for STATICFILES_DIRS:

Set Url pattern

Now, we add path to urlpatterns and set up location for uploaded files:

Source Code


By grokonez | March 15, 2019.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *