0
0
Djangoframework~20 mins

MEDIA_URL and MEDIA_ROOT in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Media Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What is the purpose of MEDIA_ROOT in Django?
In a Django project, what does the MEDIA_ROOT setting specify?
AThe directory where static files like CSS and JavaScript are collected.
BThe URL path where media files are accessed by users.
CThe template directory for rendering media content.
DThe absolute filesystem path where uploaded media files are stored.
Attempts:
2 left
💡 Hint
Think about where files physically live on your computer or server.
🧠 Conceptual
intermediate
1:30remaining
What does MEDIA_URL control in Django?
In Django settings, what is the role of MEDIA_URL?
AIt defines the URL prefix used to serve media files to users.
BIt sets the filesystem path where media files are saved.
CIt configures the database location for media metadata.
DIt specifies the URL for static files like images and CSS.
Attempts:
2 left
💡 Hint
Think about how users access uploaded files via a web address.
component_behavior
advanced
2: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?
ADjango raises an error when trying to save the uploaded file.
BThe file uploads but is saved in a temporary system folder.
CThe file is saved in the default static files directory instead.
DThe file uploads successfully but cannot be accessed via MEDIA_URL.
Attempts:
2 left
💡 Hint
Think about what happens if the folder to save files does not exist.
📝 Syntax
advanced
2: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?
A
MEDIA_URL = '/static/media/'
MEDIA_ROOT = 'media/'
B
MEDIA_URL = 'media/'
MEDIA_ROOT = '/media/'
C
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
D
MEDIA_URL = '/media'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/media')
Attempts:
2 left
💡 Hint
MEDIA_URL should start and end with a slash, and MEDIA_ROOT should be an absolute path.
state_output
expert
2: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">
A<img src="/var/www/myproject/media/uploads/photo.jpg" alt="User photo">
B<img src="/media/uploads/photo.jpg" alt="User photo">
C<img src="uploads/photo.jpg" alt="User photo">
D<img src="/static/uploads/photo.jpg" alt="User photo">
Attempts:
2 left
💡 Hint
Remember that image.url uses MEDIA_URL as the base URL path.