处理已弃用的`adminmedia`模板标签和{%admin_media_prefix%}的适当方法

时间:2022-03-27 19:20:59

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

从django 1.5开始,https://docs.djangoproject.com/en/1.5/releases/1.5/#iscellaneous

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.

模板标签库adminmedia仅包含已弃用的模板标签{%admin_media_prefix%},已被删除。尝试使用{%load adminmedia%}加载它将失败。如果您的模板仍包含该行,则必须将其删除。

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

那么,替换旧版库中的代码以及仍使用{%load adminmedia%}并加载css的遗留项目的适当方法是什么: -

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

?

2 个解决方案

#1


22  

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

从Django 1.3开始,您可以使用django.contrib.staticfiles应用程序。

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.

确保您的INSTALLED_APPS中包含django.contrib.staticfiles,并在settings.py中指定了STATIC_ROOT和STATIC_URL选项。

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

然后运行manage.py collectstatic命令,所有应用程序的静态文件将收集在STATIC_ROOT文件夹中。

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.

在模板中,您可以使用{{STATIC_URL}}上下文变量(确保django.core.context_processors.static包含在TEMPLATE_CONTEXT_PROCESSORS中)或{%static%}模板标记。

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

or

要么

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

#2


4  

I just copied what's in base.css:

我刚刚复制了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)

(将base.css替换为您需要的任何内容,例如在您的情况下使用login.css)

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

确保你的INSTALLED_APPS中有django.contrib.staticfiles。

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

(我不需要像Anton先前建议的那样配置STATIC_ROOT并运行manage.py collectstatic)

#1


22  

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

从Django 1.3开始,您可以使用django.contrib.staticfiles应用程序。

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.

确保您的INSTALLED_APPS中包含django.contrib.staticfiles,并在settings.py中指定了STATIC_ROOT和STATIC_URL选项。

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

然后运行manage.py collectstatic命令,所有应用程序的静态文件将收集在STATIC_ROOT文件夹中。

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.

在模板中,您可以使用{{STATIC_URL}}上下文变量(确保django.core.context_processors.static包含在TEMPLATE_CONTEXT_PROCESSORS中)或{%static%}模板标记。

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

or

要么

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

#2


4  

I just copied what's in base.css:

我刚刚复制了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)

(将base.css替换为您需要的任何内容,例如在您的情况下使用login.css)

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

确保你的INSTALLED_APPS中有django.contrib.staticfiles。

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

(我不需要像Anton先前建议的那样配置STATIC_ROOT并运行manage.py collectstatic)