Multiple static media roots?

2,032 views
Skip to first unread message

John Baker

unread,
Jan 24, 2009, 8:29:57 AM1/24/09
to Django users
I need to separate static media roots into "static" (belonging to
application - css, images etc) and "dynamic" (uploaded by users with
filefields etc).

The point is that truly "static" media doesn't change and is part of
the deployed code. Dynamic "static" media is uploaded and kept in a
separate directory not within the application code.

Is there any way to have 2 media directories in django?

Ideally I would like /uploads/ and /media/ but the FileField appends
the filename to the MEDIA_URL which in my case would be wrong because
it is dynamic media not truly static.

I next tried mapping 2 instances of django.views.static.serve, once
to /media/uploads/ (this solves FileField problem) and once to /media/
for truly static application media.

However, I find that django.views.static.serve doesn't allow a media
root only document root to server from.

To me it seems poor design to combine application media (belongs to a
specific version of application code) with uploaded media (belongs to
what is in the database).

Any suggestions?

Raffaele Salmaso

unread,
Jan 24, 2009, 8:36:23 AM1/24/09
to django...@googlegroups.com
John Baker wrote:
> Any suggestions?
write a custom file storage
http://docs.djangoproject.com/en/dev/topics/files/

--
()_() | That said, I didn't actually _test_ my patch. | +----
(o.o) | That's what users are for! | +---+
'm m' | (Linus Torvalds) | O |
(___) | raffaele at salmaso punto org |

Kenneth Gonsalves

unread,
Jan 24, 2009, 8:51:25 AM1/24/09
to django...@googlegroups.com
On Saturday 24 Jan 2009 6:59:57 pm John Baker wrote:
> I need to separate static media roots into "static" (belonging to
> application - css, images etc) and "dynamic" (uploaded by users with
> filefields etc).
>
> The point is that truly "static" media doesn't change and is part of
> the deployed code. Dynamic "static" media is uploaded and kept in a
> separate directory not within the application code.

I have 2 - one is the standard media directory where users upload files and is
outside the project directory, the other I call sitemedia which is a
directory under the the project directory with three directories under it -
css, js and images. This is mapped in apache conf to
http://myproject.com/sitemedia/ - and it is referred to as /sitemedia/ in the
templates. This sitemedia directory is also under version control as part of
the site.

--
regards
KG
http://lawgon.livejournal.com

John Baker

unread,
Jan 24, 2009, 9:43:54 AM1/24/09
to Django users

> write a custom file storagehttp://docs.djangoproject.com/en/dev/topics/files/

Thanks. However, I know I can store them somewhere else but my problem
is serving them up again in the local development environment. The
django.views.static.serve doesn't take an argument for serving from a
different system directory so its difficult to have 2.

Daniel Roseman

unread,
Jan 24, 2009, 10:47:20 AM1/24/09
to Django users
Er, yes it does. For example:
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': '/path/to/media'}),
(r'^uploads/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': '/path/to/uploads'}),

In both cases, /path/to/whatever can be anywhere you like, and you can
have as many as you want.

See http://docs.djangoproject.com/en/dev/howto/static-files/
--
DR.

John Baker

unread,
Jan 24, 2009, 1:09:32 PM1/24/09
to Django users
OK thanks and yes it does but that's half the story. I haven't made
myself very clear. The ImageField still tries to append /media/ to the
image upload_to path rather than /uploads/ which would retrieve them
from the dynamic media root rather than the application media.

My point was how do I get these 2 things to work together serving
multiple media directories and FileFields?

The only way I can see is to have uploads under media which I don't
really want. I can't see a way in the docs to tell file field which
media root it belongs to.

Malcolm Tredinnick

unread,
Jan 26, 2009, 9:41:48 PM1/26/09
to django...@googlegroups.com
On Sat, 2009-01-24 at 10:09 -0800, John Baker wrote:
> OK thanks and yes it does but that's half the story. I haven't made
> myself very clear. The ImageField still tries to append /media/ to the
> image upload_to path rather than /uploads/ which would retrieve them
> from the dynamic media root rather than the application media.
>
> My point was how do I get these 2 things to work together serving
> multiple media directories and FileFields?
>
> The only way I can see is to have uploads under media which I don't
> really want. I can't see a way in the docs to tell file field which
> media root it belongs to.

I still don't think you've explained the problem you're having here.
ImageField and FileField using the upload_to path (and the MEDIA_ROOT
setting) to determine where to store things.

However, you can serve static media from anywhere you like. The
MEDIA_URL setting is used for uploaded media (stuff that is stored under
MEDIA_ROOT), since there's code inside django that uses MEDIA_ROOT for
file reading/write and MEDIA_URL for the URL of those resources. But
that doesn't mean you can't also have static stuff under any other URL
you like. You just need to use a different setting to refer to it.

The only reason Django has the MEDIA_URL setting is because it's a
necessary half of allowing file uploads. It's not required to put all
static media under that URL prefix.

So what is the difficulty here? What is the problem with referring to
your unchanging (non-uploaded) static content in templates, whether via
a string in the source somewhere, or using a different settings variable
of your own naming? Is it conceptual: you're trying to do everything
using MEDIA_URL?

Regards,
Malcolm

Reply all
Reply to author
Forward
0 new messages