media files with django dev-server

159 views
Skip to first unread message

patrickk

unread,
Oct 24, 2006, 12:15:03 PM10/24/06
to django...@googlegroups.com
I know, this has been asked (and answered) before but I just can´t
display my media-files with the dev-server.

I´m getting the following error:
Page not found: /var/www/vhosts/myserver.at/django_src/django/contrib/
admin/media/uploads/...
because in the source-code, the URL to the image uses port 8000

I tried this, but it didn´t work:
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': '/var/www/vhosts/myserver.at/media/'}),

btw, we´re on apache using lighttpd for our media-directory.

I´m becoming desperate ...
patrick

orestis

unread,
Oct 24, 2006, 1:11:24 PM10/24/06
to Django users
You mention dev-server, yet you talk about apache and lighttpd.

If running the dev-server, you shouldn't do anything, since it figures
out itself how to serve files.

If you're running apache, either with mod_python or with mod_fcgi, you
should just create a link to the /foo/bar/admin/media directory in your
www directory.

You then have to edit settings.py to tell the admin where to look for
its media.

patrickk

unread,
Oct 24, 2006, 1:22:14 PM10/24/06
to django...@googlegroups.com

Am 24.10.2006 um 19:11 schrieb orestis:

>
> You mention dev-server, yet you talk about apache and lighttpd.
>
> If running the dev-server, you shouldn't do anything, since it figures
> out itself how to serve files.

yes, but it looks for media-files in django/contrib/admin/media/ ...
I´d prefer not to switch my media-directory between development- and
production-mode since there are files uploaded by users ... and I´m
not able to see these files with the dev-server.

>
> If you're running apache, either with mod_python or with mod_fcgi, you
> should just create a link to the /foo/bar/admin/media directory in
> your
> www directory.

that´s done. in production-mode, I´m not having any problems.

I´m sorry if there´s a misunderstanding.
my basic question is: how can I change the directory where the dev-
server looks for media-files?

patrick

patrickk

unread,
Oct 25, 2006, 4:30:27 AM10/25/06
to django...@googlegroups.com
I made a symlink from /django/contrib/admin/media/ to my media-
directory to solve this issue.
it works, but it doesn´t seem clean - e.g., when I do "svn update" I
guess I´m losing the changes to the media-directory.

is there any better solution?

thanks,
patrick

orestis

unread,
Oct 25, 2006, 4:41:20 AM10/25/06
to Django users
I think you have confused some concepts. /django/contrib/admin/media/
hosts the admin media files.

You mention content uploaded by users... In what directory is this put
under ?

Static files are documented here:
http://www.djangoproject.com/documentation/static_files/

patrickk

unread,
Oct 25, 2006, 4:49:08 AM10/25/06
to django...@googlegroups.com

Am 25.10.2006 um 10:41 schrieb orestis:

>
> I think you have confused some concepts. /django/contrib/admin/media/
> hosts the admin media files.
>
> You mention content uploaded by users... In what directory is this put
> under ?

/media/uploads/

>
> Static files are documented here:
> http://www.djangoproject.com/documentation/static_files/

tried that. doesn´t work for me.

I know that I´m missing something here, but I just can´t figure out
how it should work.
when starting the dev-server, every image on my site refers to the
server-path /django/contrib/admin/media/ ...

thanks,
patrick

>
>
> >

patrickk

unread,
Oct 25, 2006, 8:13:21 AM10/25/06
to django...@googlegroups.com
sorry for being a pain in the neck, but we´re about to go online with
our site and I desperately need to solve this problem.

solutions I had so far:
1. hardcoding media-urls incl. the host (not nice)
2. symlink from /django/contrib/media/ to /media/ (problem with
django-updates)

short description again:
in my source-code I´m having this
<img src="/media/uploads/userprofiles/2006/10/210/tn_profil.jpg" />

the image is uploaded by a user.
this image is not displayed because it´s not in /django/contrib/admin/
media/, but the dev-server looks there.
so, I want the dev-server to look for this image in /media/uploads/
instead.

thanks,
patrick

Malcolm Tredinnick

unread,
Oct 25, 2006, 8:42:13 AM10/25/06
to django...@googlegroups.com
On Wed, 2006-10-25 at 14:13 +0200, patrickk wrote:
> sorry for being a pain in the neck, but we´re about to go online with
> our site and I desperately need to solve this problem.

You already mentioned that you have solved it using lighttpd (or, at
least, that's what you seem to have mentioned), so if things are really
that tight, use lighttpd for your development. Spending (apparently)
hours just to try and use the django development server in your chosen
configuration seems like time wasted in your current circumstances.

> solutions I had so far:
> 1. hardcoding media-urls incl. the host (not nice)
> 2. symlink from /django/contrib/media/ to /media/ (problem with
> django-updates)

Why would you do the symlink in that direction? Link /media to the
Django source tree, then things served from /media are read from teh
Django source.

> short description again:
> in my source-code I´m having this
> <img src="/media/uploads/userprofiles/2006/10/210/tn_profil.jpg" />

You seem to be jumbling up a few different uses of "media" throughout
all this. If you are using the /media/ prefix to serve admin media, why
not put the uploaded files under another URL prefix? It looks like some
of your problems might come from trying to insist that both uploaded
files and admin media are served from "/media/".

Also note that you can change the prefix that the admin system uses to
look for it's media (it's in settings.py), so you could remove the name
clash that way.

> the image is uploaded by a user.
> this image is not displayed because it´s not in /django/contrib/admin/
> media/, but the dev-server looks there.
> so, I want the dev-server to look for this image in /media/uploads/
> instead.

Regards,
Malcolm

patrickk

unread,
Oct 25, 2006, 8:58:24 AM10/25/06
to django...@googlegroups.com
thanks malcolm, I think we´re getting closer ...

Am 25.10.2006 um 14:42 schrieb Malcolm Tredinnick:

>
> On Wed, 2006-10-25 at 14:13 +0200, patrickk wrote:
>> sorry for being a pain in the neck, but we´re about to go online with
>> our site and I desperately need to solve this problem.
>
> You already mentioned that you have solved it using lighttpd (or, at
> least, that's what you seem to have mentioned), so if things are
> really
> that tight, use lighttpd for your development. Spending (apparently)
> hours just to try and use the django development server in your chosen
> configuration seems like time wasted in your current circumstances.

misunderstanding. I just mentioned that we´re using lighttpd to serve
media in production mode.

>
>> solutions I had so far:
>> 1. hardcoding media-urls incl. the host (not nice)
>> 2. symlink from /django/contrib/media/ to /media/ (problem with
>> django-updates)
>
> Why would you do the symlink in that direction? Link /media to the
> Django source tree, then things served from /media are read from teh
> Django source.
>
>> short description again:
>> in my source-code I´m having this
>> <img src="/media/uploads/userprofiles/2006/10/210/tn_profil.jpg" />
>
> You seem to be jumbling up a few different uses of "media" throughout
> all this. If you are using the /media/ prefix to serve admin media,
> why
> not put the uploaded files under another URL prefix? It looks like
> some
> of your problems might come from trying to insist that both uploaded
> files and admin media are served from "/media/".

that´s true. all our media is in /media/ ... is that not possible?

if I change ADMIN_MEDIA_PREFIX, none of the css, images etc. is
loaded with using the dev-server.

our directory-structure:
/media/css/
/media/js/
/media/uploads/
/media/img/
...

thanks,
patrick

orestis

unread,
Oct 25, 2006, 10:13:42 AM10/25/06
to Django users
Look, there are two distinct things here:

a) The admin media, which physically resides on
django/contrib/admin/media and which the dev-server serves
automagically and

b) Your own media, which you are responsible of serving, either in
production or development mode

Here's what I do:
For admin media:
ADMIN_MEDIA_PREFIX = '/media/'

this results in the following url in admin (generated):

<link rel="stylesheet" type="text/css" href="/media/css/dashboard.css"
/>

For my own media, I use the following url (hand coded):

href="/static/a/b/c/style.css"

Then, for the development server:

Admin media: Do nothing
My media: add the following line to the main urls.py

urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': 'D:/orestis/django/static/'}),
)

Where 'D:/orestis/django/static/' is the directory which holds the
media.

For the production server:

Admin media: Create a link named "media" under the root www directory
that points to django/contrib/admin/media
My media: copy the static directory mentioned above to the root www
directory. Comment the above added line in urls.py

So your www directory should have:

media (symlink)
static (directory with your media)

Then you have to instruct your webserver to deal with those two
directories as it would, ie. without any django processing.

...
I hope this clears things up.

Istvan Albert

unread,
Oct 25, 2006, 11:29:50 AM10/25/06
to Django users

There is an option to manage.py

--adminmedia=ADMIN_MEDIA_PATH

that might just do what you need.

Malcolm Tredinnick

unread,
Oct 25, 2006, 9:35:27 PM10/25/06
to django...@googlegroups.com
On Wed, 2006-10-25 at 14:58 +0200, patrickk wrote:
> thanks malcolm, I think we´re getting closer ...
>
> Am 25.10.2006 um 14:42 schrieb Malcolm Tredinnick:
>
> >
> > On Wed, 2006-10-25 at 14:13 +0200, patrickk wrote:
> >> sorry for being a pain in the neck, but we´re about to go online with
> >> our site and I desperately need to solve this problem.
> >
> > You already mentioned that you have solved it using lighttpd (or, at
> > least, that's what you seem to have mentioned), so if things are
> > really
> > that tight, use lighttpd for your development. Spending (apparently)
> > hours just to try and use the django development server in your chosen
> > configuration seems like time wasted in your current circumstances.
>
> misunderstanding. I just mentioned that we´re using lighttpd to serve
> media in production mode.

This is all very confusing, since it's not at all clear what you have
working and what you don't. If you already have something that works in
production mode, then the simplest solution of all is to replicate that
on your development environment.

>
> >
> >> solutions I had so far:
> >> 1. hardcoding media-urls incl. the host (not nice)
> >> 2. symlink from /django/contrib/media/ to /media/ (problem with
> >> django-updates)
> >
> > Why would you do the symlink in that direction? Link /media to the
> > Django source tree, then things served from /media are read from teh
> > Django source.
> >
> >> short description again:
> >> in my source-code I´m having this
> >> <img src="/media/uploads/userprofiles/2006/10/210/tn_profil.jpg" />
> >
> > You seem to be jumbling up a few different uses of "media" throughout
> > all this. If you are using the /media/ prefix to serve admin media,
> > why
> > not put the uploaded files under another URL prefix? It looks like
> > some
> > of your problems might come from trying to insist that both uploaded
> > files and admin media are served from "/media/".
>
> that´s true. all our media is in /media/ ... is that not possible?

It may not be possible with the development server. Since the
development server is designed to be very simplistic, it doesn't have
all the functionality of a full-blown web server. It just doesn't need
it.

> if I change ADMIN_MEDIA_PREFIX, none of the css, images etc. is
> loaded with using the dev-server.

Strange. I've had that working before. And if you look in
django/core/servers/basehttp.py, in AdminMediaHandler, you can see how
the development server intercepts calls to the ADMIN_MEDIA_PREFIX
correctly. Still, that's all a bit of a side issue.

> our directory-structure:
> /media/css/
> /media/js/
> /media/uploads/
> /media/img/

I've got a very similar setup working under Apache, but there are more
than just one or two lines of configuration involved to get the right
things being served from the right location. So I wouldn't expect this
to work under the dev server (because of the way AdminMediaHandler
works).

You've given yourself an impossible set of constraints here. You want:

(1) to use the dev server
(2) no changes when moving to production
(3) to have core and non-core content served out of the
ADMIN_MEDIA_PREFIX prefix.

You can't have (1) and (3) without changing Django (you could write your
own AdminMediaHandler class, but that would take more time), so if you
want (1) in development and (3) in production, you can't have (2) as
well. Setting up Apache or lighthttpd locally is not that hard. Just
bite the bullet. Please!

Regards,
Malcolm


patrickk

unread,
Oct 26, 2006, 8:58:24 AM10/26/06
to django...@googlegroups.com
thanks everybody for the patience and the detailed answers.

using --adminmedia might be a solution. I can´t check that, because
on that specific server we´re on 0.95.

guess I´m giving up on this issue and stay with the symlink from /
django/contrib/admin/media/.

still, some notes below.

Am 26.10.2006 um 03:35 schrieb Malcolm Tredinnick:

>
> On Wed, 2006-10-25 at 14:58 +0200, patrickk wrote:
>> thanks malcolm, I think we´re getting closer ...
>>
>> Am 25.10.2006 um 14:42 schrieb Malcolm Tredinnick:
>>
>>>
>>> On Wed, 2006-10-25 at 14:13 +0200, patrickk wrote:
>>>> sorry for being a pain in the neck, but we´re about to go online
>>>> with
>>>> our site and I desperately need to solve this problem.
>>>
>>> You already mentioned that you have solved it using lighttpd (or, at
>>> least, that's what you seem to have mentioned), so if things are
>>> really
>>> that tight, use lighttpd for your development. Spending (apparently)
>>> hours just to try and use the django development server in your
>>> chosen
>>> configuration seems like time wasted in your current circumstances.
>>
>> misunderstanding. I just mentioned that we´re using lighttpd to serve
>> media in production mode.
>
> This is all very confusing, since it's not at all clear what you have
> working and what you don't. If you already have something that
> works in
> production mode, then the simplest solution of all is to replicate
> that
> on your development environment.

production is working, development not.
of course, starting the dev-server won´t work in the production
environment also.

setup on production/development is 100% the same (it´s even the same
server).

1 and 2 is true (especially 2).
3 not ... I don´t care serving admin_media with a different prefix.

>
> You can't have (1) and (3) without changing Django (you could write
> your
> own AdminMediaHandler class, but that would take more time), so if you
> want (1) in development and (3) in production, you can't have (2) as
> well. Setting up Apache or lighthttpd locally is not that hard. Just
> bite the bullet. Please!

we have a managed server - not possible to change the setup since it
´s already done and works fine in production.

thanks,
patrick

>
> Regards,
> Malcolm
>
>
>
> >

orestis

unread,
Oct 26, 2006, 10:21:41 AM10/26/06
to Django users
> > (1) to use the dev server
> > (2) no changes when moving to production
> > (3) to have core and non-core content served out of the
> > ADMIN_MEDIA_PREFIX prefix.
>
> 1 and 2 is true (especially 2).
> 3 not ... I don´t care serving admin_media with a different prefix.
>

Why don't you follow my advice then ? It works 100% for your case.

you can change the admin_media_prefix to /admin_media/, and you can
have your media files under /media/ (as it is now)

You have been presented several solutions that work. It's up to you now
:)

patrickk

unread,
Oct 28, 2006, 9:24:17 PM10/28/06
to django...@googlegroups.com

Am 26.10.2006 um 16:21 schrieb orestis:

>
>>> (1) to use the dev server
>>> (2) no changes when moving to production
>>> (3) to have core and non-core content served out of the
>>> ADMIN_MEDIA_PREFIX prefix.
>>
>> 1 and 2 is true (especially 2).
>> 3 not ... I don´t care serving admin_media with a different prefix.
>>
>
> Why don't you follow my advice then ? It works 100% for your case.

because I´d prefer the dev-server not to serve media.
I´d like the dev-server to ignore the whole media-stuff.

>
> you can change the admin_media_prefix to /admin_media/, and you can
> have your media files under /media/ (as it is now)

guess I should change that anyway (independent from the mentioned
problem).

>
> You have been presented several solutions that work. It's up to you
> now
> :)

thanks for the solutions.
patrick

>
>
> >

Reply all
Reply to author
Forward
0 new messages