How to Upload Multiple Files in Django?

Published On: 05/09/2022 | Category: Django

Hi Dev,

This tutorial shows you django multiple file upload example. you can see django multiple files upload. This tutorial will give you simple example of multiple file upload django. you will learn django multiple file upload with preview. follow bellow step for how to multiple upload pdf in django.

Here, we will install django and create a simple form with a file input field that you can use to select multiple files. after submitting the form we will store those files in a folder and database.

Django framework the file data is store into request.FILES. you need a FileField form that a view will handle this form and this form will receive file data in request.FILES which is a dictionary containing a key for each FileField. So let’s say if your form has an input field with name file and form has an attribute enctype="multipart/form-data", then file data can be accessible as request.FILES['file']. If your form does not have attribute enctype="multipart/form-data" then request.FILES['file'] will be empty.

Here i explained simply step by step example of django multiple file upload example in database.

Step 1: Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example
Step 2: Create a App

Now we'll create a single app called core to store a list of files name. We're keeping things intentionally basic. Stop the local server with Control+c and use the startapp command to create this new app.

python3 startapp core
Step 3: Update

In this step we require to do two things in our is register our new app in INSTALLED_APPS within our second is to configure our media folder. Add the below lines to your file:

Next, you need to add it in the file as follows:
import os


MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = "/media/"
Step 4: Create a Model

In this step we will require the database model for storing contacts.Open the core/ file and add the following code:

from django.db import models

class Document(models.Model):
    file = models.FileField(upload_to="documents/%Y/%m/%d")

Next, you need to migrate your database using the following command:

python makemigrations
python migrate
Step 5: Create a Form

In this step We need to create a form that will be used .like add a bootstrap class and validation etc.. plus we need to add custom styling.

from django import forms
from .models import *
from django.utils.translation import gettext as _

class DocumentForm(forms.ModelForm):

    file = forms.FileField(label=_('Choose File'),
                            error_messages = {'required':'The file field is required.','invalid':_("Files only")}, 
                            widget=forms.FileInput(attrs={'multiple':True, 'class': 'form-control custom-file-input','id' : 'customFile'})

    class Meta:
        model = Document
        fields = ['file',]
Step 6: Creating the Views

In this step, we need to configure our views. The file_upload page will just be a template and store to multiple image database and our media folder.So, open the core/ file and add:

from django.shortcuts import render
from .forms import *
from .models import *
from django.contrib import messages
import os

# Create your views here.

def file_upload(request):

    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        files = request.FILES.getlist('file')

        if form.is_valid():
            for file in files:
                ext = os.path.splitext([1]
                valid_extensions = ['.pdf', '.csv', '.doc', '.docx', '.xlsx', '.xlx']
                if ext.lower() in valid_extensions:

            if ext.lower() in valid_extensions:
                messages.success(request, 'File Upload Successfully')
                messages.error(request, 'Unsupported file extension!')
        form = DocumentForm()

    return render(request, 'index.html', {'form' : form})
Step 7: Creating the Template

Next, open the core/templates/index.html file and the add:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <link rel="stylesheet" href="[email protected]/dist/css/bootstrap.min.css">
    <script src="[email protected]/dist/js/bootstrap.bundle.min.js"></script>
    <script src=""></script>
    <style type="text/css">
            background-color: #f7fcff;
    <div class="container mt-5 pt-5">
        <div class="row d-flex justify-content-center">
            <div class="col-md-8">
                {% if messages %}
                    <div class="alert alert-success alert-dismissible">
                        <button type="button" class="close" data-dismiss="alert">×</button>
                        {% for message in messages %}
                            {{ message }}
                        {% endfor %}
                {% endif %}
                <div class="card">
                    <div class="card-header">
                        <h4>How to Multiple File Upload in Django? - <span class="text-primary"></span></h4>
                    <div class="card-body">
                        <form method = "post" enctype="multipart/form-data" novalidate>
                            {% csrf_token %}
                            <div class="custom-file">
                                {% for field in form %}
                                    <label class="custom-file-label" for="customFile">{{ field.label }}</label>
                                    {{ field }}
                                    <small class="text-danger mt-2">{{ field.errors|striptags }}</small>
                                {% endfor %}
                            <small class="mt-2">(File extension must be pdf, csv, doc, docx, xlsx, xlx format.)</small>
                            <div class="row mt-4">
                                <div class="col-lg-12 text-center">
                                    <button class="btn btn-success">Submit</button>
<script type="text/javascript">

    Custom File Input 
    $(".custom-file-input").on("change", function() {
        var fileName = $(this).val().split("\\").pop();
Step 8: Creating Urls

In this section, we need a file within the core app however Django doesn't create one for us with the startapp command. Create core/ with your text editor and paste below code:

from django.urls import path
from . import views

urlpatterns = [
    path('files/', views.file_upload, name="file_upload"),

Next, we require to add a URL path for our example project which can be done by importing include and setting a path for it.

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path("", include('core.urls')),

if settings.DEBUG:
        urlpatterns += static(settings.MEDIA_URL,

Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python runserver

Next, go to the http://localhost:8000/files address with a web browser.

I Hope It will help you....