Django TemplateSyntaxError-'staticfiles'不是注册的标记库


78

升级到Django 3.0后,我得到以下信息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

这是我的模板

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

Answers:


167

{% load staticfiles %}{% load admin_static %}分别在Django 2.1弃用,并且在Django 3.0移除

如果模板中包含以下任何内容:

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

您应该将标签替换为:

{% load static %}

5
  • 尝试{% load static %}代替{% load staticfiles %}
  • 如果CSS或任何其他文件的效果未反映在模板中,请在settings.py文件末尾写以下行
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')

1
添加STATICFILES_DIRS = [os.path.join(BASE_DIR,'static')]解决了此问题。在向模板中的DIRS添加静态文件目录之前,这是行不通的。非常感谢!
k0rnik

应该将其标记为正确答案。现在其他选项已废弃。
Alex8752

1

这与我一起工作替换{% load static from staticfiles %}{% load static %}

在哪里:

转到您的虚拟环境“ venv” /lip/python3.X/site-packages/leaflet/templates/leaflet/admin/widget.html和所有。目录中的HTML文件


1
更改文件site-packages是一个坏主意。在您的情况下,最好将django-leaflet升级到支持Django 3.0+的较新版本
Alasdair
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.