MEDIA_ROOT is the absolute path on your server where Django saves uploaded files. It is not a URL but a filesystem location.
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.
MEDIA_ROOT is set to a non-existent directory. What will happen when a user uploads a file?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.
MEDIA_URL and MEDIA_ROOT for serving user-uploaded files?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.
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">
image.url uses MEDIA_URL as the base URL path.The image.url property combines MEDIA_URL with the file's relative path inside MEDIA_ROOT. So the URL is '/media/uploads/photo.jpg'.
