Hosting Admin Media Locally During Development
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.
If you're running Grappelli, you can set the admin media when using runserver
by specifying the command line argument --adminmedia
. Here is an example:
python manage.py runserver --adminmedia=/path/to/grappelli/media
Where /path/to/grappelli/media
is the complete path to your Grappelli installation's media
directory.
You don't need to specifically serve the admin media while using the development server - this should happen automatically.
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.
精彩评论