问题描述
从django 1.5起,
From django 1.5 onwards, https://docs.djangoproject.com/en/1.5/releases/1.5/#miscellaneous
那么,什么是适当的方式来替换在旧的图书馆和我的遗留项目中找到的代码仍然使用 {%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,您可以使用 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%}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!