简体   繁体   中英

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' %}">

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 .

  • 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')

This worked for me using django 3.1.4.

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

its working for me

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

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' %}">

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.

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