我有一个 Django 项目,最初我在 base.html 文件中编写了所有 HTML 和模板代码。然而,随着我的项目的发展,我决定通过将特定代码移动到单独的模板文件来更好地组织我的模板。我创建了一个名为sports.html的新模板文件,并将相关代码从base.html复制到sports.html。但是,当我在浏览器中查看sports.html页面时,我只看到base.html的内容被渲染,而我移动到sports.html的代码没有显示。
这是我的 base.html 文件的结构:
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Head content here -->
</head>
<body>
<!-- Header, sidebar, footer content here -->
{% block content %}
{% endblock %}
<!-- Footer content here -->
</body>
</html>
这是我的 sports.html 文件的结构:
`{% extends 'BTXApp/base.html' %}`
`{% block content %} `
`<!-- My content code goes here --> <!-- But it's not showing up in the rendered page --> `
`{% endblock %}`
我已经仔细检查了 Django 设置中的路径和配置,一切似乎都是正确的。我可以在我的文本编辑器(Sublime Text)中完美地看到代码,但它没有在浏览器中呈现。
什么可能导致此问题?如何解决?任何帮助或见解将不胜感激。谢谢!
如果你想从项目中模板文件夹中的模板扩展,你应该只写
base.html
并在你的 settings.py 中这样写:
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates'],
'APP_DIRS': True,
.
.
.
],
},
},
]```
you should have templates folder in your project folder like this:(if your Django project named A) A/templates/base.html
even after this, your app's template structure should be like this: (yourappname)/templates/(yourappname)/(the place for your templates related to this app)