Appropriate way to handle deprecated `adminmedia` templatetag and {% admin_media_prefix %}
Asked Answered
C

2

24

From django 1.5 onwards, https://docs.djangoproject.com/en/1.5/releases/1.5/#miscellaneous

The template tags library adminmedia, which only contained the deprecated template tag {% admin_media_prefix %}, was removed. Attempting to load it with {% load adminmedia %} will fail. If your templates still contain that line you must remove it.

So what is the appropriate way to replace code found in legacy libraries and my legacy projects which still uses {% load adminmedia %} and loads css like:-

<link rel="stylesheet" type="text/css" href="{% load adminmedia %}{% admin_media_prefix %}css/login.css">

?

Cupel answered 29/11, 2012 at 11:11 Comment(0)
B
24

Since Django 1.3 you can use django.contrib.staticfiles app.

Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS and the STATIC_ROOT and STATIC_URL options are specified in your settings.py.

Then run manage.py collectstatic command and all applications' static files will be collected in STATIC_ROOT folder.

In the templates you can use the {{ STATIC_URL }} context variable (make sure that django.core.context_processors.static is included in TEMPLATE_CONTEXT_PROCESSORS) or the {% static %} template tag.

<link href="{{ STATIC_URL }}admin/css/login.css" rel="stylesheet">

or

{% load staticfiles %}
<link href="{% static 'admin/css/login.css' %}" rel="stylesheet">
Brunt answered 29/11, 2012 at 13:36 Comment(3)
I am looking about the docs for STATIC_PATH and can't see anything, should that be STATIC_ROOT?Fetlock
@Fetlock You're right about STATIC_ROOT, I've submitted an edit for this answer (awaiting peer review).Sizing
Is pulling those files into your own project really the recommended solution? It sounds like a hack.Spinal
L
5

I just copied what's in base.css:

{% load admin_static %}

and then

<link href="{% static 'admin/css/base.css' %}" rel="stylesheet">

(replace base.css with whatever you need, like login.css in your case)

Make sure you have django.contrib.staticfiles in your INSTALLED_APPS.

(I didn't need to configure STATIC_ROOT and run manage.py collectstatic as suggested previously by Anton)

Luiseluiza answered 19/5, 2013 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.