Member-only story
How to Upload Files and Images in Your Django Application
Django file and image uploads

Uploading files and images occur every day, whether you are sending documents to your work colleagues or uploading media to your favorite social media platform.
In this guide, you’ll learn to upload files and images in your Django apps. You will also learn how to serve files from your Django application.
Project Setup
We will start by creating a directory where our project and a virtual environment will live.
Create a directory, cd to the directory and create a virtual environment:
mkdir filesDjango
cd filesDjango
python3.8 -m venv env
Virtual environments are recommended to keep the project dependencies separate from the os. Activate the virtual environment and install Django in the virtual environment:
source env/bin/activatepip install Django
Create a new Django project called file uploads:
django-admin startproject fileuploads
Inside the Django project directory, create an app called files. Apps in Django are used to separate different components and are essential for scaling apps.
Apps are also movable parts that can be moved to another Django project without breaking the code.
django-admin startapp files
Add the app files to the list of installed apps in settings.py
file
INSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','files',]
Django stores files locally, with the MEDIA_ROOT and MEDIA_URL settings. Let’s define these constants in settings.py
import osMEDIA_URL = "/media/"MEDIA_ROOT = os.path.join(BASE_DIR, 'media')