tags:

views:

187

answers:

2

Consider the following setup:

urls.py

if not settings.PRODUCTION:
    urlpatterns += patterns('',
        (r'^admin-media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.LOCAL_ADMIN_MEDIA_ROOT, 'show_indexes': True}),
        (r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.LOCAL_MEDIA_ROOT, 'show_indexes': True}),
    )

settings.py

if not PRODUCTION:
    ADMIN_MEDIA_PREFIX = '/admin-media/'

So when running on the local development server the media files should be served through the runserver, right? The media route is found, however "Permission denied" is returned for every request (but only one admin media, the regular media works fine).

So I did some checking into it. It turns out, if the ADMIN_MEDIA_PREFIX is set to the same value as the route...

(r'^admin-media/(?P<path>.*)$', 'django.views.static.serve', ... ),
ADMIN_MEDIA_PREFIX = '/admin-media/'

...then the runserver will always return "Permission denied."

However, if ADMIN_MEDIA_PREFIX is different than the route name...

(r'^admin-media/(?P<path>.*)$', 'django.views.static.serve', ... ),
ADMIN_MEDIA_PREFIX = '/non-sense-prefix/'

... then the files will be served (though I have to manually browse to be able to see them as all the media links are broken with http://localhost:8000/non-sense-prefix/whatever.jpg).

What's the deal here?

In the mean time I've solved the problem by a little hack to change directories...

(r'^admin-media/(?P<path>.*)$', 'django.views.static.serve', ... ),
ADMIN_MEDIA_PREFIX = '/admin-media/../admin-media/'

...but I would really rather configure this properly. It seems django is trying to be smart and do something on my behalf, but messing things up in the process. Any ideas?

EDIT -- I'm manually serving the admin media because I'm using grappelli which provides replacement for the admin templates/media.

A: 

You don't need to specifically serve the admin media while using the development server - this should happen automatically.

Daniel Roseman
I'm using grappelli which has replacement templates/media files instead of the default django admin ones.
T. Stone
A: 
  • I think it is simpler to just symlink in your local system to the django admin media, the same directory that you use in production, than to subject to a lot of if PRODUCTION within settings.

  • As Daniel rightly pointed out, django serves admin media by default. Anymore configuration is not required. So the problem is perhaps something different. Try chmod 777 on the templates directory, It may fix the problems.

Lakshman Prasad
I had considered the symlink route (as NT+ supports them), however everyone else who checks out the repo would have to do the same. I had checked the permissions as well, and they're correct.
T. Stone