Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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"> 

?

like image 543
Calvin Cheng Avatar asked Nov 29 '12 11:11

Calvin Cheng


1 Answers

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"> 
like image 177
Anton Morozov Avatar answered Dec 06 '22 03:12

Anton Morozov