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 to
STATIC_URL
, but as you might guess, it's the URL that should be used to serve media. It must end in a/
. Generally, you will use something like/media/
.Note
For security reasons, the path for
MEDIA_ROOT
must not be the same as the path forSTATIC_ROOT
, andMEDIA_URL
must not be the same asSTATIC_URL
. If they were the same, a user might replace your static files (such as JavaScript or CSS files) with malicious...