0
0
Djangoframework~20 mins

Serving media in development in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Media Mastery in Django
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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 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?
AIt allows Django's development server to serve media files from MEDIA_ROOT at MEDIA_URL automatically.
BIt configures Django to serve static files from STATIC_ROOT during development.
CIt disables serving media files and requires a separate web server for media.
DIt causes a runtime error because static() cannot be used with MEDIA_URL.
Attempts:
2 left
💡 Hint
Think about how Django handles static and media files differently in development.
📝 Syntax
intermediate
2:00remaining
Correct syntax to serve media files in Django development
Which of the following urls.py snippets correctly enables serving media files during development?
Aurlpatterns = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Burlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Curlpatterns += static(settings.STATIC_URL, document_root=settings.MEDIA_ROOT)
Durlpatterns += static(settings.MEDIA_ROOT, document_root=settings.MEDIA_URL)
Attempts:
2 left
💡 Hint
Remember the order of arguments for static() and what MEDIA_URL and MEDIA_ROOT represent.
🔧 Debug
advanced
2: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?
AThe static() function only works for static files, not media files.
BMEDIA_ROOT is incorrectly set to a URL instead of a filesystem path.
CYou forgot to add <code>MEDIA_URL</code> to <code>INSTALLED_APPS</code>.
DDEBUG is set to False, so Django does not serve media files automatically.
Attempts:
2 left
💡 Hint
Check the DEBUG setting and how Django behaves differently in production.
🧠 Conceptual
advanced
2:00remaining
Understanding the role of MEDIA_URL and MEDIA_ROOT
In Django, what is the difference between MEDIA_URL and MEDIA_ROOT?
ABoth <code>MEDIA_URL</code> and <code>MEDIA_ROOT</code> are URL prefixes for different types of media.
B<code>MEDIA_URL</code> is the filesystem path; <code>MEDIA_ROOT</code> is the URL prefix for media files.
C<code>MEDIA_URL</code> is the URL prefix for media files; <code>MEDIA_ROOT</code> is the filesystem path where media files are stored.
D<code>MEDIA_URL</code> is used only in production; <code>MEDIA_ROOT</code> is used only in development.
Attempts:
2 left
💡 Hint
Think about where files live on disk versus how they are accessed via a browser.
state_output
expert
2:00remaining
What is the output when accessing media URL with missing file?
Given this Django development setup:
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?
ADjango returns a 404 Not Found error page because the file does not exist.
BDjango serves a default placeholder image automatically.
CDjango raises a server error (500) because the file is missing.
DDjango redirects the request to the homepage.
Attempts:
2 left
💡 Hint
What does a web server usually do when a requested file is missing?