Working and Configuring Media Files in Django.

Abdullah zulfiqar
Django Unleashed
Published in
3 min readFeb 5, 2024

I’ve seen that beginners in Django often get confused about something called ‘media’. They ask questions on places like StackOverflow, and get good answers, but these answers don’t always explain what actually media are and why media is used. I’m here to make it easy for you to understand media files in Django and how to set them up.

Image representing media separated in different directories with a centralised media directory.

Before we dive in, I’m assuming you already know the basics of Django.

Objective:

  • Explaining how media actually works in Django.
  • How to setup media files dir and how to serve media.

What are Media files?

Media files on your website refer to files that users upload, such as a profile picture, which is a common example. However, there can be various other types of media, including PDF documents, Excel spreadsheets, and more. In simple terms, any file uploaded through a FileField in Django can be considered a media file.

Django offers a large degree of flexibility to manage user uploads as there are always security concerns when dealing with user uploaded content.

Remember, it’s really important to validate all the files users upload are safe.

What does “serving” media even mean?

Serving media files means making certain media files available for your website or app to use. Like when users login they should see their profile picture right-away.

First, start by collecting all your media files in a single folder, typically named ‘media.’ To do this, create a ‘media’ directory within your project’s base directory. Next, make sure to define the path to your media directory in all the FileFields within your project models. This ensures that any files uploaded by users are stored in a centralized location within the ‘media’ directory.

The ‘media’ directory should contain all your media files. Afterward, configure MEDIA_URL and MEDIA_ROOT for this directory. This setup enables you to make your media files accessible.

Configuring MEDIA_URL and MEDIA_ROOT for Serving Media Files:

First opensettings.py file of your project and add the following configuration.

# Define the base URL for serving media files
MEDIA_URL = '/media/'

# Specify the directory where media files are stored
MEDIA_ROOT = BASE_DIR / 'media'

In older versions of Django, which used the ‘os’ module to navigate paths, you would do the following instead:

MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')

What does Media URL actually do?

Media url is the web address where your website stores and retrieves any kind of media files like above we discussed the example of profile picture. For example, if your website’s domain is example.com and you set your Media URL to /media/, then your media files can be found at example.com/media/.

During website development, the Media URL choice isn’t important as long as it works. But, when the website is live, using a service like Amazon S3 for media files is better for faster and more reliable performance, especially on websites with heavy traffic.

What does Media ROOT actually mean?

MEDIA_ROOT refers to the specific folder location where uploaded files are stored.

Always remember that whether Debug is set to True or False, Django does not automatically serve media files.

Lets setup the settings for development server when debug=True. Open the urls.py of the project and add the below changes.

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
path('admin/', admin.site.urls),
...]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)

This setting means that your media files will be accessible via URLs like localhost:8000/media/example.png during development. Think of it as a reference to your media files.

Now that everything is set up, start the local development server. You can then add files to the MEDIA_ROOT folder and access them using the media URL.

Why You Need to Set MEDIA_URL and MEDIA_ROOT for Production or When Using Servers Other than Django’s Development Server?

Django does not automatically serve media files in production, so it’s necessary to configure Django to know the location of these files by specifying the MEDIA_ROOT and MEDIA_URL settings.

However, as mentioned earlier, using this setup is neither safe nor efficient for a production environment. It’s recommended to use a service like Amazon S3 for hosting media files in production to ensure better security and performance.

--

--

Abdullah zulfiqar
Django Unleashed

Software Engineer Python | Full Stack Architect | Python Engineer | Django | Flask | FastAPI | AI | Freelance