Ala*_*air 112 python django django-templates django-3.0
升级到 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
Run Code Online (Sandbox Code Playgroud)
这是我的模板
{% load staticfiles %}
<img src="{% static 'my_image.html' %}">
Run Code Online (Sandbox Code Playgroud)
Ala*_*air 266
如果您的模板中有以下任何标签:
{% load staticfiles %}
{% load static from staticfiles %}
{% load admin_static %}
Run Code Online (Sandbox Code Playgroud)
然后将其替换为:
{% load static %}
Run Code Online (Sandbox Code Playgroud)
您必须进行此更改,因为{% load staticfiles %}和在 Django 2.1{% load admin_static %}中已弃用,并在 Django 3.0 中删除。
小智 16
{% load static %}代替{% load staticfiles %}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')
Run Code Online (Sandbox Code Playgroud)
这对我使用 django 3.1.4 有效。
{% load static %}
<link rel"stylesheet" href = "{% static 'style.css' %}">
Run Code Online (Sandbox Code Playgroud)
staticfiles已更改为静态
您可以在您的 setting.py 中使用空闲代码进行注册
在您的 TEMPALTE 设置中添加此代码
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',
}
},
},
]
Run Code Online (Sandbox Code Playgroud)
请注意,您可以找到您没有的图书馆
| 归档时间: |
|
| 查看次数: |
92727 次 |
| 最近记录: |