MEDIA_URL Configuration
In this page:
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.
# 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.
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.
<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. #}
- Forgetting to add the media URL pattern in urls.py during development, so uploaded files 404 even though they exist on disk.
- Using django.conf.urls.static.static() in production — it's a development-only convenience, not meant for real deployments.
- Mixing up MEDIA_URL with MEDIA_ROOT when building a template link to an uploaded file.
- 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.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: