Media Files Overview
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.
# 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.
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.
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. #}
- Confusing media files (user-uploaded content) with static files (developer-provided assets) — they use separate settings.
- Forgetting that MEDIA_ROOT must exist and be writable, or uploads will fail.
- Serving user uploads directly from the project's static/ directory instead of a dedicated media directory.
- 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.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: