Integrate a Bootstrap File Upload Utility in Django

Integrating a standard file upload feature in Django is relatively straightforward. It's simply a case of rendering the individual image field in your Django template.

For a functional image selector, there's absolutely nothing wrong with this approach. It achieves its objective of replacing an image field with another image that a user selects from their computer's hard drive.

What about if you would like a full width selector with the name and image's location to be displayed within the image selector's form control? The image name should be displayed both when the user arrives at an update page, or following a selection of an image from their hard drive.

This isn't as trivial as it perhaps could or even should be and there are a couple of preconditions that must be met before implementing this. Firstly, you'll need to ensure that the Bootstrap stylesheet is referenced within your site's <head> tag and the Bootstrap's JS is referenced inside the closing </body> tag of your site.

# base.html

<html lang="en">
    <!-- Meta -->

    <!-- Bootstrap core CSS -->
    <link rel="stylesheet" 

    <!-- Bootstrap JS -->
    <script src="" 

    <!-- Additional Page Specific JavaScript -->
    {% block additional_js %}{% endblock additional_js %}


An additional script will also be required to be inserted as an additonal JavaScript block beneath your {% endblock content %} tag.

{% extends 'base.html' %}
{% load static %}

{% block content %}
  Your form's content...
{% endblock content %}

{% block additional_js %}
  <!-- Functionality for selected filename from input selector to be displayed -->
  <script src={% static 'assets/js/input-filename.js' %} 
{% endblock additional_js %}


In your create/update form code, you will need to render out each of the form fields individually. When it comes to rendering out the image field with the Bootstrap file picker, you will need to use the following code.

'form' is the name you have given to your form. The canonical way of naming your form is 'form'

'image' is the name of your image field

<!-- Post Image -->
<div class="custom-file mt-2">
  <input type="file" class="custom-file-input" name="image" accept="image/*" 
    id="{{ form.image.id_for_label }}">
  <label class="custom-file-label" for="{{ form.image.id_for_label }}">
    {% if post.image %}{{ }}{% else %}Select Post Image...{% endif %}
  <div class="small text-muted">{{ form.image.help_text }}</div>
  <span class="text-danger small">{{ form.image.errors }}</span>

The JavaScript code that makes this work is:

  var fileName =[0].name;

Reference Details


You can find my implementation of this at the following commit: Reference GitHub Code

Use GitHub's 'Go to file' button to look for the following files:

  • pages/templates/pages/base.html
  • pages/templates/pages/js.html
  • blog/templates/blog/components/post_create.html
  • blog/templates/blog/components/post_image_update.html
  • static/assets/js/input-filename.js

Here are the relevant Boostrap Docs

Pinned Versions: Python 3.8.3, Django 3.0.7, Bootstrap 4.5.0