Challenge - 5 Problems
Media Mastery in Django
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
How does Django serve media files during development?
In a Django project running with DEBUG=True, what happens when you add this to your
What is the effect of this code on serving media files?
urls.py?from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# your url patterns
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)What is the effect of this code on serving media files?
Attempts:
2 left
💡 Hint
Think about how Django handles static and media files differently in development.
✗ Incorrect
The static() helper function is designed to serve files from MEDIA_ROOT at MEDIA_URL during development when DEBUG=True. This is a convenience so you don't need a separate web server for media files while developing.
📝 Syntax
intermediate2:00remaining
Correct syntax to serve media files in Django development
Which of the following
urls.py snippets correctly enables serving media files during development?Attempts:
2 left
💡 Hint
Remember the order of arguments for static() and what MEDIA_URL and MEDIA_ROOT represent.
✗ Incorrect
The static() function takes the URL prefix first (MEDIA_URL) and the filesystem path second (MEDIA_ROOT). You append the result to urlpatterns to add media serving routes.
🔧 Debug
advanced2:00remaining
Why are media files not served despite correct settings?
You have added
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) in your urls.py and set MEDIA_URL = '/media/' and MEDIA_ROOT = BASE_DIR / 'media' in settings.py. But when you try to access /media/image.jpg, you get a 404 error. What is the most likely cause?Attempts:
2 left
💡 Hint
Check the DEBUG setting and how Django behaves differently in production.
✗ Incorrect
Django only serves media files automatically during development when DEBUG=True. If DEBUG=False, Django expects a real web server to serve media files.
🧠 Conceptual
advanced2:00remaining
Understanding the role of MEDIA_URL and MEDIA_ROOT
In Django, what is the difference between
MEDIA_URL and MEDIA_ROOT?Attempts:
2 left
💡 Hint
Think about where files live on disk versus how they are accessed via a browser.
✗ Incorrect
MEDIA_ROOT is the folder on your computer or server where uploaded media files are saved. MEDIA_URL is the web address prefix used to access those files in a browser.❓ state_output
expert2:00remaining
What is the output when accessing media URL with missing file?
Given this Django development setup:
If you request
DEBUG = True MEDIA_URL = '/media/' MEDIA_ROOT = BASE_DIR / 'media' urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
If you request
/media/nonexistent.jpg in the browser, what will happen?Attempts:
2 left
💡 Hint
What does a web server usually do when a requested file is missing?
✗ Incorrect
When a file is not found in MEDIA_ROOT, Django's development server returns a 404 Not Found error to the browser.