简体   繁体   English

Django django.contrib.staticfiles.templatetags.static 已在 3.0 中删除:如何替换该功能?

[英]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.

相关问题 没有名为“django.contrib.staticfiles.templatetags”的模块 - No module named 'django.contrib.staticfiles.templatetags' 升级到 Django 3.0:“django.contrib.admin.templatetags.admin_static”:无法导入名称“RemovedInDjango30Warning” - Upgrading to Django 3.0: 'django.contrib.admin.templatetags.admin_static': cannot import name 'RemovedInDjango30Warning' 如何在Django 1.4中使用django.contrib.staticfiles提供管理静态文件(使用一个Apache服务器)? - How to serve admin static files with django.contrib.staticfiles in Django 1.4 (using one Apache server)? 如何在静态媒体文件中使用Django templatetags? - How to use Django templatetags in static media files? &#39;django.contrib.staticfiles&#39;的问题, - Issues with 'django.contrib.staticfiles', 我如何设置Debian x86_64,以便Django项目看到django.contrib.staticfiles? - How can I set up Debian x86_64 so a Django project sees django.contrib.staticfiles? 如何在 Django 中配置我的静态文件? - how do I configure my staticfiles in django? 我不太明白 STATICFILES_DIRS 和 STATIC_ROOT Django 设置是如何工作的 - I don't really understand how STATICFILES_DIRS and STATIC_ROOT Django settings works django.contrib.staticfiles.finders.find需要帮助 - django.contrib.staticfiles.finders.find help needed Django的; 如何为自定义templatetags做条件 - Django; How to do conditionals for custom templatetags
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM