Member-only story

How to Upload Files and Images in Your Django Application

Esther Vaati
Better Programming
Published in
5 min readApr 6, 2022

Photo by: Matthew Henry

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')

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Esther Vaati
Esther Vaati

Written by Esther Vaati

Get practical JavaScript tips and code snippets delivered to your inbox. Join 1000 + developers who write better code. https://practicaljavascript.substack.com/

Responses (2)

Write a response