Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 333
Can Savastürk Avatar asked Sep 02 '25 16:09

Can Savastürk


2 Answers

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

like image 189
Hasan Agha Avatar answered Sep 04 '25 04:09

Hasan Agha


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.

like image 33
dirkgroten Avatar answered Sep 04 '25 06:09

dirkgroten