0
0
DjangoHow-ToBeginner · 3 min read

How to Configure Media Files in Django: Step-by-Step Guide

To configure media files in Django, set MEDIA_URL and MEDIA_ROOT in your settings.py. Then, add URL patterns in urls.py to serve media files during development using django.conf.urls.static.static.
📐

Syntax

To configure media files in Django, you need to define two settings in settings.py:

  • MEDIA_URL: The URL prefix for media files.
  • MEDIA_ROOT: The absolute filesystem path where media files are stored.

In urls.py, you add a URL pattern to serve media files during development using static() from django.conf.urls.static.

python
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'

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

urlpatterns = [
    # your url patterns here
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
💻

Example

This example shows a minimal Django project setup to handle media files. It configures MEDIA_URL and MEDIA_ROOT in settings.py, and updates urls.py to serve media files during development.

python
# settings.py
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent

MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'

# urls.py
from django.contrib import admin
from django.urls import path
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)
Output
When running the Django development server, media files uploaded to the 'media' folder are accessible at URLs starting with '/media/'. For example, an image saved as 'media/photo.jpg' is accessible at 'http://localhost:8000/media/photo.jpg'.
⚠️

Common Pitfalls

  • Forgetting to set MEDIA_ROOT to an absolute path causes media files not to save correctly.
  • Not adding the static() helper in urls.py means media files won't be served during development.
  • Trying to serve media files with Django in production is inefficient; use a dedicated web server instead.
  • Using MEDIA_URL without trailing slash can cause URL issues.
python
# Wrong: MEDIA_ROOT is relative path (may cause errors)
MEDIA_ROOT = 'media'

# Right: MEDIA_ROOT is absolute path
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
MEDIA_ROOT = BASE_DIR / 'media'

# Wrong: Missing static() in urls.py
urlpatterns = [
    path('admin/', admin.site.urls),
]

# Right: Add static() for media files
from django.conf.urls.static import static
from django.conf import settings

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
📊

Quick Reference

Summary tips for configuring media files in Django:

  • Set MEDIA_URL with a trailing slash, e.g. '/media/'.
  • Set MEDIA_ROOT as an absolute path to your media folder.
  • Use static() in urls.py only when DEBUG=True to serve media files during development.
  • In production, serve media files with a web server like Nginx or Apache.

Key Takeaways

Always set MEDIA_URL and MEDIA_ROOT in settings.py to configure media files.
Use django.conf.urls.static.static in urls.py to serve media files during development.
MEDIA_ROOT must be an absolute path to avoid file saving issues.
Never serve media files with Django in production; use a dedicated web server.
Ensure MEDIA_URL ends with a trailing slash to avoid URL problems.