适当的方法来处理已弃用的“adminmedia”模板和{%admin_media_prefix%} [英] Appropriate way to handle deprecated `adminmedia` templatetag and {% admin_media_prefix %}

查看:185
本文介绍了适当的方法来处理已弃用的“adminmedia”模板和{%admin_media_prefix%}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从django 1.5起, https://docs.djangoproject.com/en/1.5/releases /1.5/#miscellaneous

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


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

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.

那么,什么是适当的方式来替换在旧的图书馆和我的遗留项目中找到的代码仍然使用 {%load adminmedia%} 并加载css,如: -

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

推荐答案

由于Django 1.3,您可以使用 django.contrib.staticfiles app。

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

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.

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

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

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

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

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

这篇关于适当的方法来处理已弃用的“adminmedia”模板和{%admin_media_prefix%}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆