Skip to content Skip to sidebar Skip to footer

Django Admin Media Prefix Url Issue

i 've the following folder structure src\BAT\templates\admin\base.html src\BAT\media\base.css src\BAT\media\admin-media\base.css settings.py MEDIA_ROOT = os.path.join( APP_DIR, 'm

Solution 1:

Important for Django 1.4 and newer (see here):

Starting in Django 1.4, the admin’s static files also follow this convention, to make the files easier to deploy. In previous versions of Django, it was also common to define an ADMIN_MEDIA_PREFIX setting to point to the URL where the admin’s static files live on a Web server. This setting has now been deprecated and replaced by the more general setting STATIC_URL. Django will now expect to find the admin static files under the URL <STATIC_URL>/admin/.


Previous answer, for older Django releases:

ADMIN_MEDIA_PREFIX is meant to be an absolute URL prefix, it has nothing to do with the MEDIA_URL - both can point to completely different points. Admittedly, the (bad) choice of "_PREFIX" in the name somewhat suggests that.

So, instead of {{ MEDIA_URL }}{{ADMIN_MEDIA_PREFIX}}css/base.css it must be {% admin_media_prefix %}css/base.css. And then you have to ensure that the web server serves the admin media files on '/admin-media/'.

Note that I used the admin_media_prefix tag above, which needs {% load adminmedia %} at the beginning of the template. The regular media context processor only gives you the MEDIA_URL variable, unfortunately.

In order to override the vanilla admin media serving, try something like this in your URLconf:

# A handy helper functionIalwaysuseforsite-relativepathsdeffromRelativePath(*relativeComponents):
    returnos.path.join(os.path.dirname(__file__), *relativeComponents).replace("\\","/")

[...]

url("^admin-media/(?P<path>.*)$",
    "django.views.static.serve",
    {"document_root": fromRelativePath("media", "admin-media")})

Solution 2:

Django 1.4 uses a new strategy for loading static media files, those using it will want to read over https://docs.djangoproject.com/en/dev/howto/static-files/

The executive summary of the above link is that two new settings variables, STATIC_URL and STATIC_ROOT, are used together with a newly included app (django.contrib.staticfiles) to collect and serve static files which are included on a per app basis.

When upgrading my django installation I had to set my STATIC_ROOT equal to my previous MEDIA_URL.

Under this system templates should now use {{ STATIC_URL }}.

Post a Comment for "Django Admin Media Prefix Url Issue"