Serving media in development
📖 Scenario: You are building a Django website that allows users to upload profile pictures. During development, you want to serve these uploaded images from your local computer so you can see them in the browser without setting up a full web server.
🎯 Goal: Set up Django to serve media files during development by configuring the media URL and root, and updating the URL patterns to serve media files correctly.
📋 What You'll Learn
Create a
MEDIA_URL setting with the value "/media/" in settings.pyCreate a
MEDIA_ROOT setting pointing to a directory named media inside the project base directoryImport
static and settings in urls.pyAdd a URL pattern to serve media files during development using
static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)💡 Why This Matters
🌍 Real World
Websites often allow users to upload images or files. During development, serving these files locally helps you test and see changes immediately.
💼 Career
Knowing how to serve media files in Django is essential for backend developers working on user-generated content features.
Progress0 / 4 steps