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

MEDIA_URL Configuration

MEDIA_URL tells the browser where to go looking for uploaded files, and in development Django needs one extra line to actually hand those files out.

Setting MEDIA_URL and MEDIA_ROOT

MEDIA_URL is the public URL prefix for uploads; MEDIA_ROOT is the filesystem directory they're actually saved to. Both are required for media uploads to work.

Example: Setting MEDIA_URL and MEDIA_ROOT

MEDIA_URL is the public URL prefix for uploads; MEDIA_ROOT is the filesystem directory they're actually saved to. Both are required for media uploads to work.

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. #}

Serving Media Files in Development

Unlike static files, media files need an explicit URL pattern added in development so Django knows to serve them from MEDIA_ROOT.

Note: This pattern should only be added when DEBUG is True — it's not meant for production use.

Example: Serving Media Files in Development

Unlike static files, media files need an explicit URL pattern added in development so Django knows to serve them from MEDIA_ROOT.

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

urlpatterns = [
    # ... your other patterns
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
{# 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. #}

Referencing an Uploaded File in a Template

A FileField/ImageField's .url attribute already includes MEDIA_URL, so templates can link directly to it without manual string building.

Example: Referencing an Uploaded File in a Template

A FileField/ImageField's .url attribute already includes MEDIA_URL, so templates can link directly to it without manual string building.

markup
<img src="{{ profile.avatar.url }}" alt="Avatar">
{# 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. Forgetting to add the media URL pattern in urls.py during development, so uploaded files 404 even though they exist on disk.
  2. Using django.conf.urls.static.static() in production — it's a development-only convenience, not meant for real deployments.
  3. Mixing up MEDIA_URL with MEDIA_ROOT when building a template link to an uploaded file.
Chapter Summary
  • MEDIA_URL is the URL prefix used to serve user-uploaded files.
  • In development, urls.py must explicitly add a pattern to serve files from MEDIA_ROOT.
  • In production, a real web server (not Django itself) should serve media files.
  • A model's FileField/ImageField already builds the correct URL through its .url attribute.

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.