简体   繁体   中英

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

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 %} . Obviously for source code I will need another alternative.

django.contrib.staticfiles.templatetags was deprecated in Django's version 2.1 . And now it has been completely removed from version 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() . If you use the latter, everything will work as you expect it.

See the Django 2.1 release notes , when this was deprecated.

Here's what I did to get my project static files working on Django 3.0:

Previously in settings.py I had the following:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM