[英]Django django.contrib.staticfiles.templatetags.static removed in 3.0: How could I replace the functionality?
I have the following code block where a corresponding .css
file's path is returned.我有以下代码块,其中返回相应的.css
文件的路径。
It is part of a Theme-Class
that allows the user to change the website theme (dark and light) from a button in the profile view.它是Theme-Class
一部分,允许用户通过配置文件视图中的按钮更改网站主题(深色和浅色)。
def link(self) -> str:
"""
Returns the link where the CSS file for this theme is located
"""
return static('app_shared/colors_%s.css' % self.name())
The same problem when it occurs in an HTML-Template can be solved by changing {% load staticfiles %}
to {% load static %}
.当它发生在 HTML 模板中时,同样的问题可以通过将{% load staticfiles %}
更改为{% load static %}
。 Obviously for source code I will need another alternative.显然,对于源代码,我需要另一种选择。
django.contrib.staticfiles.templatetags was deprecated in Django's version 2.1 . django.contrib.staticfiles.templatetags 在 Django 的2.1 版中被弃用。 And now it has been completely removed from version 3 .现在它已从版本 3 中完全删除。
Simply, replace简单地,替换
from django.contrib.staticfiles.templatetags.staticfiles import static
with和
from django.templatetags.static import static
Hope this will help...希望这会有所帮助...
You're misunderstanding what's being removed.您误解了要删除的内容。 django.contrib.staticfiles.templatetags.static()
is deprecated in favor of django.templatetags.static.static()
.不推荐使用django.contrib.staticfiles.templatetags.static()
以支持django.templatetags.static.static()
。 If you use the latter, everything will work as you expect it.如果您使用后者,一切都会如您所愿。
See the Django 2.1 release notes , when this was deprecated.请参阅Django 2.1 发行说明,当它被弃用时。
Here's what I did to get my project static files working on Django 3.0:这是我为让我的项目静态文件在 Django 3.0 上运行所做的工作:
Previously in settings.py I had the following:以前在 settings.py 中我有以下内容:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
'builtins': [
'django.contrib.staticfiles.templatetags.staticfiles',
]
},
},
]
Because of the change you mentioned, now I have:由于您提到的更改,现在我有:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
'builtins': [
'django.templatetags.static', # <------------ New way
]
},
},
]
and it works again.它再次起作用。
I kind of wish this was just a standard part of Django as this has been pretty much necessary in all of my projects, but alas, this works.我有点希望这只是 Django 的一个标准部分,因为这在我所有的项目中都是非常必要的,但是,唉,这是有效的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.