ChatGPT解决这个技术问题 Extra ChatGPT

Django TemplateSyntaxError - 'staticfiles' is not a registered tag library

After upgrading to Django 3.0, I get the following TemplateSyntaxError:

In template /Users/alasdair//myproject/myapp/templates/index.html, error at line 1
'staticfiles' is not a registered tag library. Must be one of: admin_list admin_modify admin_urls cache i18n l10n log static tz

Here is my template

{% load staticfiles %}
<img src="{% static 'my_image.html' %}">

A
Alasdair

If you have any of the following tags in your template:

{% load staticfiles %}
{% load static from staticfiles %}
{% load admin_static %}

Then replace it with:

{% load static %}

You have to make this change because {% load staticfiles %} and {% load admin_static %} were deprecated in Django 2.1, and removed in Django 3.0.


Unfortunately it did not work seamlessly with Django==3.0.5, djangorestframework==3.11.0, and django_rest_swagger==2.2.0. It would require a collectstatic followed by a replace.
g
gue

Register staticfiles to tag library

staticfiles has been change to static

You can register with the fallowing code in your settings.py

Add this code in your TEMPLATES settings:

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',
            ],
            'libraries' : {
                'staticfiles': 'django.templatetags.static', 
            }
        },
    },
]

Note the you can find libraries witch you don't have it


T
Tonechas

Try {% load static %} instead of {% load staticfiles %}

If effect of CSS or any other files doesn't reflect in your template then also write following lines in the end of your settings.py file

STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
VENV_PATH = os.path.dirname(BASE_DIR)
STATIC_ROOT = os.path.join(VENV_PATH, 'static_root')

Adding STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')] solved the issue. Before I was adding static file directory to DIRS inside templates which didn't work. Many thanks!
This should be tagged as the correct answer. The other options are now obsoleted.
Does the CSS part work in windows 7 64bit also? I tried the same but giving error
C
Community

This worked for me using django 3.1.4.

{% load static %}
<link rel"stylesheet" href = "{% static 'style.css' %}">

its working for me


M
MoShamroukh

this worked with me replace {% load static from staticfiles %} with {% load static %}

where :

go to your virtual environment "venv" /lip/python3.X/site-packages/leaflet/templates/leaflet/admin/widget.html and all of the .HTML files in the directory


Changing files in site-packages is a bad idea. In your case, it would be better to upgrade django-leaflet to a newer version that supports Django 3.0+
D
DonkeyKong

My issue was in step 4 from documentation here: https://docs.djangoproject.com/en/3.2/howto/static-files/

"4. Store your static files in a folder called static in your app. For example my_app/static/my_app/example.jpg."

Once I had corrected my directory structure to match everything worked.


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now