0
0
Djangoframework~10 mins

MEDIA_URL and MEDIA_ROOT in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - MEDIA_URL and MEDIA_ROOT
Start Django settings
Define MEDIA_ROOT
Define MEDIA_URL
User uploads file
File saved to MEDIA_ROOT
File accessed via MEDIA_URL
Browser loads media file
This flow shows how Django settings MEDIA_ROOT and MEDIA_URL work together to save and serve user-uploaded files.
Execution Sample
Django
MEDIA_ROOT = BASE_DIR / 'media'
MEDIA_URL = '/media/'

# User uploads 'photo.jpg'
# File saved to MEDIA_ROOT / 'photo.jpg'
# Accessed at MEDIA_URL + 'photo.jpg'
Defines where files are stored and how URLs map to those files for user uploads.
Execution Table
StepActionVariable/SettingValueEffect
1Define MEDIA_ROOTMEDIA_ROOTBASE_DIR / 'media'Files saved here on disk
2Define MEDIA_URLMEDIA_URL'/media/'URL prefix for media files
3User uploads fileuploaded_file'photo.jpg'File ready to save
4Save filefile_pathMEDIA_ROOT / 'photo.jpg'File stored on server
5Access filefile_urlMEDIA_URL + 'photo.jpg'Browser URL to load file
6Browser requests fileHTTP requestGET /media/photo.jpgServer serves file from MEDIA_ROOT
7EndFile served successfully
💡 File served via MEDIA_URL from location MEDIA_ROOT
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
MEDIA_ROOTBASE_DIR / 'media'BASE_DIR / 'media'BASE_DIR / 'media'BASE_DIR / 'media'BASE_DIR / 'media'
MEDIA_URL'/media/''/media/''/media/''/media/''/media/'
uploaded_fileNone'photo.jpg''photo.jpg''photo.jpg''photo.jpg'
file_pathNoneNoneMEDIA_ROOT / 'photo.jpg'MEDIA_ROOT / 'photo.jpg'MEDIA_ROOT / 'photo.jpg'
file_urlNoneNoneNone'/media/photo.jpg''/media/photo.jpg'
Key Moments - 2 Insights
Why do we need both MEDIA_ROOT and MEDIA_URL?
MEDIA_ROOT tells Django where to save files on the server disk (see step 4). MEDIA_URL is the web address prefix to access those files in a browser (see step 5). Both work together to store and serve files.
What happens if MEDIA_URL does not match the URL pattern serving media files?
If MEDIA_URL is not configured correctly, the browser URL (step 5) won't find the file, causing a 404 error. The execution_table step 6 shows the browser requesting the file URL; if it fails, the file won't load.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of MEDIA_ROOT after step 4?
ABASE_DIR / 'media'
B'/media/'
CNone
DBASE_DIR / 'static'
💡 Hint
Check the 'Variable/Setting' and 'Value' columns at step 4 in the execution_table.
At which step does the file get saved on the server disk?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look for the action 'Save file' in the execution_table.
If MEDIA_URL was set to '/files/' instead of '/media/', what would change in the execution table?
Afile_path at step 4 would change
Buploaded_file name would change
Cfile_url at step 5 would be '/files/photo.jpg'
DMEDIA_ROOT would change
💡 Hint
MEDIA_URL affects the URL used to access files, see step 5 in execution_table.
Concept Snapshot
MEDIA_ROOT sets the folder on your server where uploaded files are saved.
MEDIA_URL is the web address prefix used to access those files.
When a user uploads a file, Django saves it to MEDIA_ROOT.
The file is accessed in a browser via MEDIA_URL + filename.
Both must be set correctly for media files to work.
MEDIA_ROOT is a file path; MEDIA_URL is a URL path.
Full Transcript
In Django, MEDIA_ROOT is the folder path on the server where user-uploaded files are stored. MEDIA_URL is the URL prefix used to access these files from a browser. When a user uploads a file, Django saves it inside MEDIA_ROOT. To access the file, the browser uses MEDIA_URL plus the filename. For example, if MEDIA_ROOT is BASE_DIR / 'media' and MEDIA_URL is '/media/', a file named photo.jpg is saved to BASE_DIR/media/photo.jpg and accessed at /media/photo.jpg in the browser. This setup requires both settings to be configured correctly. The execution table shows each step from defining these settings, uploading the file, saving it, and finally serving it to the browser. Understanding this flow helps avoid common mistakes like 404 errors when media files don't load.