MEDIA_ROOT and MEDIA_URL

11 views
Skip to first unread message

David S.

unread,
Oct 28, 2005, 9:20:24 AM10/28/05
to django...@googlegroups.com
I do not understand precisely how these two settings are used.

From Django doc, we have:
MEDIA_ROOT - Absolute path to the directory that holds media for this
installation. Example: "/home/media/media.lawrence.com/" See also MEDIA_URL.
MEDIA_URL - URL that handles the media served from MEDIA_ROOT. Example:
"http://media.lawrence.com"

And from another post:
MEDIA_ROOT tells Django where it should put uploaded images.
MEDIA_URL tells Django the prefix it should use in media-references.

Is MEDIA_ROOT really only for uploaded images (or, generally, files)? The 2
descriptions of MEDIA_URL seem contradictory. How, when, and where is it
actually applied?

Thanks.

P.S. Django impresses more and more. It has become so easy to put solid and
consistent web apps together. So, here is to pragmatism--cheers!

Eugene Lazutkin

unread,
Oct 28, 2005, 1:06:44 PM10/28/05
to django...@googlegroups.com
Prefix 'MEDIA_' is a little bit misleading. MEDIA_* stuff is for uploaded
files (FileField and ImageField). MEDIA_ROOT specifies a directory where you
want to put uploaded files. MEDIA_URL is how to access them from browser.
Basically it boils down to these steps:

1) Create a directory for uploaded files. Example: /home/yourname/myuploads/
2) Specify MEDIA_ROOT in your settings.py: MEDIA_ROOT =
'/home/yourname/myuploads/'
3) Map it in your web server to URL you want. Example: you can access these
files using http://site.my/upload/
4) Specify MEDIA_URL in your settings.py: MEDIA_URL =
'http://site.my/upload/'

As you can see you can easily use a different web server on different
computer to serve your uploaded files --- it's great for scalability.
Obviously in this case you should use some network file sharing.

This is what I do for #3 using Apache:
a) I made a symbolic link from my web site directory to the upload
directory: ln -s /home/name/upload upload
b) I modified .htaccess file adding: RewriteRule ^(upload/.*)$ - [L]

Now when I browse to this directory (http://lazutkin.com/upload/) I can see
files I have here (you can turn off directory browsing later). I don't need
Django to serve this directory --- it's done by Apache. But I need to tell
Django how to put files there (MEDIA_ROOT) and how to form urls to show
these files (MEDIA_URL). So file is going to be saved as MEDIA_ROOT/abc.txt
and it's going to be accessed as MEDIA_URL/abc.txt.

I hope it helps,

Eugene



"David S." <david...@alumni.tufts.edu> wrote in message
news:loom.2005102...@post.gmane.org...

David S.

unread,
Oct 28, 2005, 1:47:11 PM10/28/05
to django...@googlegroups.com
Eugene Lazutkin <eugene.lazutkin <at> gmail.com> writes:

>
>
> Prefix 'MEDIA_' is a little bit misleading. MEDIA_* stuff is for uploaded
> files (FileField and ImageField). MEDIA_ROOT specifies a directory where you
> want to put uploaded files. MEDIA_URL is how to access them from browser.

Thank you and thanks to the offline responses.

Just to clarify then: these MEDIA_* settings are only relevent for upload files,
specifically fields of type FileField and ImageField. And all other things
sometimes refered to as media in Django docs, like css, js, etc., are just
handled by the http server, correct?

Eugene Lazutkin

unread,
Oct 28, 2005, 3:01:15 PM10/28/05
to django...@googlegroups.com
Yes. You are correct.

"David S." <david...@alumni.tufts.edu> wrote in message
news:loom.2005102...@post.gmane.org...
>
Reply all
Reply to author
Forward
0 new messages