Django uses MEDIA_URL and MEDIA_ROOT to manage user-uploaded files like images, videos, and documents. These settings define where files are stored and how they are accessed.
MEDIA_URL defines the public URL, while MEDIA_ROOT defines the server directory where files are saved.
# URL prefix used to access media files
MEDIA_URL = '/media/'
# Directory where uploaded files are stored
import os
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# Display uploaded image and document
Download
# Enable media serving in development
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# Nginx configuration for media files
location /media/ {
alias /path/to/project/media/;
}
Modify the values below to see the difference between where Django saves a file versus how it serves it.