Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: serving ADMIN media files

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 :)

like image 545
Adrian Liem Avatar asked Jul 04 '09 05:07

Adrian Liem


People also ask

How do I serve media files in Django?

To make Django development server serve static we have to add a URL pattern in sitewide urls.py file. Now visit http://127.0.0.1:8000/media/python.png again, this time you should be able to see the image. Just as with static files, in the production, you should always use a real web server to serve media files.

How does Django find static files?

Using the collectstatic command, Django looks for all static files in your apps and collects them wherever you told it to, i.e. the STATIC_ROOT . In our case, we are telling Django that when we run python manage.py collectstatic , gather all static files into a folder called staticfiles in our project root directory.


2 Answers

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.

like image 50
phoku Avatar answered Oct 04 '22 15:10

phoku


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

like image 32
zalun Avatar answered Oct 04 '22 15:10

zalun