19

I've been successfully serving media files for the normal MEDIA files, but when I tried serving admin media files, I failed. please kindly help me locating the problem, as I've tried to troubleshoot the problem for several hours already with no luck (been googling too and read the django doc about serving static files as well).

The error as I tried to access localhost:8000/media/a.gif is as following:

Page not found: f:\python25\lib\site-packages\django/contrib/admin/media\a.gif

I put the admin media files in directory named "media", while I put the normal media files in directory named "static". I'm on Windows, too.

Here's how I serve the ordinary media files in urls.py:

# serve static files
from django.conf import settings
if settings.ENVIRONMENT==settings.ENV_DEVELOPMENT:
    urlpatterns += patterns("django.views",
        url(r"%s(?P<path>.*)$" % settings.MEDIA_URL[1:], "static.serve", {"document_root": settings.MEDIA_ROOT,})
    )

And my settings.py (only the important pieces):

import project_path
MEDIA_ROOT = project_path.MEDIA.replace('\\','/')
MEDIA_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/media/'
TEMPLATE_DIRS = (
    project_path.TEMPLATE.replace('\\','/'),
)

And my project_path.py:

import sys
from os.path import dirname, join
ROOT = dirname(__file__)
APP = join(ROOT, "apps")
TEMPLATE = join(ROOT, "templates")
MEDIA = join(ROOT, "static")
ADMIN_MEDIA = join(ROOT, "media")

Any hints?

or maybe at least please share how do you serve your admin media files (without changing any files from the web server, but only via the django source code)

Thanks in advance :)

Adrian Liem
  • 251
  • 1
  • 3
  • 11
  • Did you ever resolve this? I am having a similar experience working on Mac with Django 1.1, serving normal media files works but not for the admin... – Danielb Aug 05 '09 at 04:45
  • The reason the url settings don't work, is because `django/core/management/commands/runserver.py` together with `django.core.servers.basehttp` setup a WSGI handler for `ADMIN_MEDIA_PREFIX`. It handles the request before the URLconf is hit – vdboor Apr 06 '12 at 12:05

7 Answers7

32

Your answer is that unless ADMIN_MEDIA_PREFIX explicitly sets a domain the runserver command will serve out the admin media files from contrib.admin.

I got burned by this magic behaviour, too. There was a ticket for this (Ticket #8336), but the design decision was to leave the convenience and confusion as it is.

So to serve your admin media (for using grappelli or whatever admin skin you want to use) from your directories with the runserver command you have to use something like:

MEDIA_ROOT =  os.path.join(PROJECT_ROOT, 'media/')
ADMIN_MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'admin-media/')
MEDIA_URL = '/site-media/'
ADMIN_MEDIA_PREFIX = 'http:/localhost:8000/admin-media/'

I hope I am resurrecting the correct question here. Apologies in advance.

phoku
  • 2,082
  • 1
  • 18
  • 16
  • 3
    Magic convenience behaviours. Thanks Django! How about sensible defaults in settings.py instead? (sigh)... thanks for this Phoku! – Rich Sep 17 '10 at 07:18
  • 1
    I think there is some logic in their decision as maybe most of the users don't change and there is already so many settings to take care of. As a beginner this kind of conveniences are nice to have. – Sam Stoelinga Feb 29 '12 at 03:54
  • The "magic" is more of a clever workaround, it curcumvents the `path.startswith(base_url)` check in `AdminMediaHandler._should_handle()`. Hence, you skip the WSGI handler, and enter the normal URLconf :) – vdboor Apr 06 '12 at 12:06
3

It is advised to run the dev server a little bit different [1] python manage.py runserver mydomain.com:8000 --adminmedia=/path/to/your/admin/media/

[1] http://code.google.com/p/django-grappelli/wiki/Installation

zalun
  • 4,619
  • 5
  • 31
  • 33
  • 2
    That must be django-grappelli specific. With vanilla Django 1.2, I get `error: no such option: --adminmedia` – Vebjorn Ljosa Mar 21 '11 at 14:02
  • No, the explanation is [here][1]: "If the staticfiles contrib app is enabled (default in new projects) the runserver command will be overriden with an own runserver command which doesn't have the --adminmedia option due to deprecation." [1]: https://docs.djangoproject.com/en/dev/ref/django-admin/ – eedeep Nov 10 '11 at 03:34
2

Try

(r'^admin_media/(.*)', 'django.views.static.serve', {'document_root' : 'django/contrib/admin/media/', 'show_indexes' : True}),

in your urls.py file. And change your

ADMIN_MEDIA_PREFIX = '/admin_media/'
Paul Tarjan
  • 48,968
  • 59
  • 172
  • 213
  • thanks for the answer, but it still doesn't work :) I typed http://localhost:8000/admin_media/a.gif in the broswer url and the result is: Page not found: f:\python25\lib\site-packages\django/contrib/admin/media\a.gif – Adrian Liem Jul 05 '09 at 14:51
2

I just fixed a similar bug in my test site. ADMIN_MEDIA_PREFIX and MEDIA_URL should never be the same, see the following note in the docs:

Make sure to use a trailing slash, and to have this be different from the MEDIA_URL setting (since the same URL cannot be mapped onto two different sets of files).

Sam Starling
  • 5,298
  • 3
  • 35
  • 52
Danielb
  • 1,608
  • 5
  • 24
  • 34
1

Try changing:

ADMIN_MEDIA_PREFIX = '/static/media/'

This assumes that your MEDIA_ROOT/media/ directory contains the admin media folder (which is what I understood from your question).

ars
  • 120,335
  • 23
  • 147
  • 134
  • thanks for the answer, but it still doesn't work :) I typed http://localhost:8000/static/media/a.gif in the broswer url and the result is: Page not found: f:\python25\lib\site-packages\django/contrib/admin/media\a.gif – Adrian Liem Jul 05 '09 at 14:51
  • I think I'll try to recreate the project from scratch again using the conventional admin media examples :) thanks for the answers – Adrian Liem Jul 05 '09 at 14:53
1

Since staticfiles inclusion into trunk, (circa 1.3 I believe), the ADMIN_MEDIA_PREFIX magic is no longer used.

Nowadays, runserver assumes your STATIC_URL + 'admin/'. It secretly and silently intercepts your requests, ignores all your urlconf's, and tries to make you go mad.

s29
  • 2,027
  • 25
  • 20
  • 1
    I found the opposite - with django 1.3 if I didn't have ADMIN_MEDIA_PREFIX = posixpath.join(STATIC_URL, "admin/") (ie, if I just didn't have ADMIN_MEDIA_PREFIX set at all) then runserver didn't serve my admin media correctly. – eedeep Nov 10 '11 at 03:20
0

Try to Use STATICFILES_DIRS as blow

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    ("images", os.path.join(STATIC_ROOT,'images').replace('\\','/')),
    ("css",    os.path.join(STATIC_ROOT,'css').replace('\\','/')),
    ("js",     os.path.join(STATIC_ROOT,'js').replace('\\','/')),
)

Have a goodluck!

HUHU
  • 1
  • 1
    STATICFILES_DIRS = ( ("media", os.path.join(STATIC_ROOT,'media').replace('\\','/')), ) – HUHU Nov 10 '11 at 08:16