← Back to Django Course | Chapter 11: Static Files & Media Handling | Lesson 5 of 8

Media Files Overview

Media files are things your users upload themselves, like a photo they attach to their profile, unlike static files which you as the developer already provide.

Media vs Static Files

Static files are shipped with your code (CSS, JS, images you designed). Media files are uploaded later by users at runtime (avatars, attachments) and need their own storage location.

Example: Media vs Static Files

Static files are shipped with your code (CSS, JS, images you designed). Media files are uploaded later by users at runtime (avatars, attachments) and need their own storage location.

markup
# settings.py
MEDIA_URL = 'media/'
MEDIA_ROOT = BASE_DIR / 'media'
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Where Uploads Are Stored

MEDIA_ROOT is the filesystem directory Django saves uploaded files into; MEDIA_URL is the URL prefix used to serve them back to the browser.

Note: Keep MEDIA_ROOT outside STATIC_ROOT so collectstatic never touches user uploads.

Example: Where Uploads Are Stored

MEDIA_ROOT is the filesystem directory Django saves uploaded files into; MEDIA_URL is the URL prefix used to serve them back to the browser.

markup
MEDIA_ROOT = BASE_DIR / 'media'
# An uploaded avatar might land at:
# media/avatars/profile1.jpg
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

A Model That Accepts Uploads

A model needs a FileField or ImageField to store an uploaded file's path in the database while the actual file lives under MEDIA_ROOT.

Example: A Model That Accepts Uploads

A model needs a FileField or ImageField to store an uploaded file's path in the database while the actual file lives under MEDIA_ROOT.

markup
from django.db import models

class Profile(models.Model):
    name = models.CharField(max_length=100)
    avatar = models.ImageField(upload_to='avatars/')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Common Mistakes
  1. Confusing media files (user-uploaded content) with static files (developer-provided assets) — they use separate settings.
  2. Forgetting that MEDIA_ROOT must exist and be writable, or uploads will fail.
  3. Serving user uploads directly from the project's static/ directory instead of a dedicated media directory.
Chapter Summary
  • Media files are user-uploaded content, such as profile pictures or attached documents.
  • MEDIA_URL and MEDIA_ROOT are the media equivalents of STATIC_URL and STATIC_ROOT.
  • Django never processes media file content — it just stores and serves whatever the user uploads.
  • Media handling requires FileField or ImageField on a model, plus enctype='multipart/form-data' on the form.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.