Export to GitHub

django-grappelli - issue #149

How to get the grappelli media files to work with the debug server?


Posted on Dec 21, 2009 by Quick Giraffe

For some reason, I am completely unable to get Grappelli working with the debug server.

Here are my settings:

MEDIA_ROOT = '/Users/Nick/Code/pmw/media' MEDIA_URL = '/media/' ADMIN_MEDIA_PREFIX = '/media/admin/'

The Grappelli media files are located at '/Users/Nick/Code/pmw/media/grappelli'.

With these settings, I'm able to successfully resolve the following:

http://localhost:8000/media/grappelli/js/admin/CollapsibleGroup.js

However, when I change ADMIN_MEDIA_PREFIX to:

ADMIN_MEDIA_PREFIX = '/media/grappelli/'

The file is no longer being served:

"Page not found: /media/grappelli/js/admin/CollapsibleGroup.js"

I'm using this code to serve static files on debug server:

if settings.DEBUG: urlpatterns += patterns('', (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT.replace('\','/')}), )

Pulling my hair out. Would love any help.

Thanks.

Comment #1

Posted on Dec 22, 2009 by Happy Monkey

a bit strange. from what I see here this should work.

however, did you try using --adminmedia ... python manage.py runserver localhost:8000 --adminmedia=/Users/Nick/Code/pmw/media/grappelli/

another thing you could try is to explicitely define your admin-media directory as static.server like this: (r'^media/grappelli/(?P.*)$', 'django.views.static.serve', {'document_root': '/Users/Nick/Code/pmw/media/grappelli/'}),

hope this helps. regards, patrick

Comment #2

Posted on Dec 22, 2009 by Quick Giraffe

Ah - good tips - I knew there had to be some server-level switch the debug server is making for admin media, as it always defaults to the django install's admin-media.

I'll give those a try.

Thanks!

Comment #3

Posted on Dec 22, 2009 by Quick Giraffe

By the way, I did try the second thing - explicitly defining the admin-media dir with the static server with no luck.

Comment #4

Posted on Dec 22, 2009 by Quick Lion

For me, using adminmedia option is kind of workaround - it works but there should be a better solution.

It would be much simpler to put additional media at, for example, 'grappelli' directory within static document_root. Templates are already overridden so implementation would be quite easy ( src={{ MEDIA_URL}}grappelli/... ), as far as I can imagine as I don't really know the impact on the admin internals.

And usage of 'adminmedia' optition should probably be pointed out at install documentation (I haven't been even aware of it's existence ;-)).

Oh, and patrick, second approach you have suggested doesn't really work, at least for me - I'm using Django 1.1. I'm trying to get it working this way for much too long and it is very frustrating...

Have this at my settings module: ADMIN_MEDIA_PREFIX = '/admin-media/'

And this at urls.py:

if settings.DEBUG: urlpatterns += patterns('django.views', # admin-media (r'^%s(?P.*)$' % settings.ADMIN_MEDIA_PREFIX[1:] , 'static.serve', { 'document_root': os.path.join(settings.PROJECT_ROOT, 'admin-media'), 'show_indexes' : True, }), ...

and its before I bind admin app.

It seems that changes at urls.py didn't go into life as I got strange error if I try to list the directory: "Permission denied: /admin-media/"...

I tried to directly run django.views.static.directory_index on my django/contrib/admin/media directory and it returned as expected (means without any error). So I'm totally lost here.

Comment #5

Posted on Jan 7, 2010 by Happy Rhino

I had this problem for a while and finally got a good understanding of what's going on.

You need to use the --adminmedia flag because otherwise django will default the admin folder to django/contrib/admin/media in your pythonpath. *And it will also set up the static serve urlconf entries automatically. This is the real problem. You can't just override the urlconf settings because the admin will still have the file system path set wrong. So you need to set the --adminmedia path as well so the urlconf looks in the right place.

The only thing I would recommend is to make the setup instructions a little more clear. The grappelli media folder completely replaces the default django media folder. So just change everything to point there and you're in good shape.

Comment #6

Posted on Jan 7, 2010 by Quick Giraffe

Ah - awesome. Thank you for the explanation. That pretty much drove me crazy for 8+ hours.

Comment #7

Posted on Jan 7, 2010 by Happy Monkey

usage of --adminmedia is in the docs now ... thanks for the hint.

but: you can put grappelli-media wherever you want to ... you just need to tell your server to use that directory as your (admin)-media-directory. of course, that´s not necessary if you use djangos built-in dev-server. without the dev-server, you need to change your server-setup (e.g., if you use /admin-media/ as your ADMIN_MEDIA_PREFIX, you need to tell your server how /admin-media/ is served).

Comment #8

Posted on Apr 7, 2010 by Quick Lion

See this answer from stackoverflow: http://stackoverflow.com/questions/1081596/django-serving-admin- media-files#1656828

It seems that the ADMIN_MEDIA_PREFIX only has effect if the full domain name is supplied: ADMIN_MEDIA_PREFIX = 'http:/localhost:8000/admin-media/'

Comment #9

Posted on Apr 8, 2010 by Happy Monkey

@dirk: we never use the full domain with ADMIN_MEDIA_PREFIX, so that can´t be an issue.

Comment #10

Posted on Sep 2, 2010 by Swift Ox

@dirk: thanks! that helped!

Status: Invalid

Labels:
Type-Defect Priority-Medium