Django{%extend static“template.html”%}抛出TemplateSyntaxError

2024-06-01 00:16:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个template.html文件,其中包含一些锅炉板html文本

然后在index.html中,我有:

{% load static %}
{% extends static 'template.html' %}

在settings.py中,我有(相关内容):

INSTALLED_APPS = [
    'notes.apps.NotesConfig', 
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

STATIC_URL = '/static/'

然而,当我试图呈现index.html时,我得到了一个 TemplateSyntaxError at 'extends' takes one argument。 当template.html与index.html放在同一目录中时,我使用了

{% extends 'template.html' %}

一切都很顺利


Tags: installed文件djangopy文本内容indexsettings
2条回答

If you use {% extends %} in a template, it must be the first template tag in that template. Template inheritance won’t work, otherwise.

我们将根据documentation来确定这一点,因此我不确定您是否可以加载static

问题是为什么要扩展static?将其移动到模板,然后以常规方式进行扩展

似乎需要添加对静态文件夹的引用,以便Django知道在哪里查找静态文件。 为此,您需要在settings.py文件中添加以下代码行。 假设在基本目录中有一个用于存放静态文件的文件夹,manage.py文件所在的位置。只需将以下行添加到settings.py中即可

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'staticfiles'),
]
# Assuming the name of your static files folder is "staticfiles"

相关问题 更多 >