Settings for media uploads and serving
In Chapter 5, Serving Static Files, we looked at how Django can be used to serve static files. Serving media files is quite similar. Two settings must be configured in settings.py
: MEDIA_ROOT
and MEDIA_URL
. These are analogous to STATIC_ROOT
and STATIC_URL
for serving static files.
MEDIA_ROOT
: This is the path on the disk where the media (such as uploaded files) will be stored. As with static files, your web server should be configured to serve directly from this directory to take the load off Django.MEDIA_URL
: This is similar toSTATIC_URL
, but as you might guess, it’s the URL that should be used to serve media. It must end in/
. Generally, you will use something such as/media/
.
Note
For security reasons, the path for MEDIA_ROOT
must not be the same as the path for STATIC_ROOT
, and MEDIA_URL
must not be the same as STATIC_URL
. If they were the same, a user might replace your static files (such as JavaScript or CSS...