Challenge - 5 Problems
Media Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:30remaining
What is the purpose of MEDIA_ROOT in Django?
In a Django project, what does the MEDIA_ROOT setting specify?
Attempts:
2 left
💡 Hint
Think about where files physically live on your computer or server.
✗ Incorrect
MEDIA_ROOT is the absolute path on your server where Django saves uploaded files. It is not a URL but a filesystem location.
🧠 Conceptual
intermediate1:30remaining
What does MEDIA_URL control in Django?
In Django settings, what is the role of MEDIA_URL?
Attempts:
2 left
💡 Hint
Think about how users access uploaded files via a web address.
✗ Incorrect
MEDIA_URL is the URL prefix that browsers use to access media files. It usually starts with a slash and points to the location served by the web server.
❓ component_behavior
advanced2:00remaining
What happens if MEDIA_ROOT is not set correctly?
Consider a Django project where
MEDIA_ROOT is set to a non-existent directory. What will happen when a user uploads a file?Attempts:
2 left
💡 Hint
Think about what happens if the folder to save files does not exist.
✗ Incorrect
If MEDIA_ROOT points to a folder that does not exist, Django cannot save the uploaded file and raises an error, usually a FileNotFoundError or IOError.
📝 Syntax
advanced2:00remaining
Identify the correct MEDIA_URL and MEDIA_ROOT settings
Which of the following Django settings correctly configures
MEDIA_URL and MEDIA_ROOT for serving user-uploaded files?Attempts:
2 left
💡 Hint
MEDIA_URL should start and end with a slash, and MEDIA_ROOT should be an absolute path.
✗ Incorrect
Correct settings use a URL path starting and ending with slashes for MEDIA_URL and an absolute filesystem path for MEDIA_ROOT. Option C follows this pattern.
❓ state_output
expert2:30remaining
What is the output of this Django media URL usage?
Given the following Django settings and template snippet, what will be the rendered HTML output for the image tag if the uploaded file is named 'photo.jpg'?
Django
settings.py: MEDIA_URL = '/media/' MEDIA_ROOT = '/var/www/myproject/media' models.py: class Photo(models.Model): image = models.ImageField(upload_to='uploads/') Template snippet: <img src="{{ photo.image.url }}" alt="User photo">
Attempts:
2 left
💡 Hint
Remember that
image.url uses MEDIA_URL as the base URL path.✗ Incorrect
The image.url property combines MEDIA_URL with the file's relative path inside MEDIA_ROOT. So the URL is '/media/uploads/photo.jpg'.