Django公司Django.contrib.staticfiles.templatetags.static在3.0中删除:如何替换功能?

2024-04-18 23:58:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码块,其中返回相应的.css文件的路径。
它是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())

在HTML模板中出现同样的问题可以通过将{% load staticfiles %}更改为{% load static %}来解决。显然,对于源代码,我需要另一种选择。在


Tags: 文件the代码用户self路径主题网站
2条回答

为了让我的项目静态文件在Django 3.0上运行,我做了如下工作:

以前在设置.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', 
            ]
        },
    },
]

因为你提到的改变,现在我有:

^{pr2}$

它又起作用了。在

我有点希望这只是Django的一个标准部分,因为这在我所有的项目中都是非常必要的,但是,唉,这很管用。在

你误解了正在删除的内容。django.contrib.staticfiles.templatetags.static()被弃用,取而代之的是{}。如果你使用后者,一切都会如你所料。在

请参阅Django 2.1 release notes,当它被弃用时。在

相关问题 更多 >