首页 > 解决方案 > Django 应用程序不包括下游应用程序中的 base.html 模板

问题描述

我有一个 Django 应用程序,我正在尝试设置一个基本的 html 模板来包含页脚、导航栏等,我可以在整个站点中重复使用这些模板。

我遇到的问题是,当我尝试将基本模板包含在下游应用程序模板文件夹和索引中时出现错误。我希望这是有道理的。

在我的主根目录中templates/base.html,在我的应用程序中,NewsBase/templates/NewsBase/index.html我有以下内容:

{% extends 'templates/base.html' %}

<body>
    <h1>NewsBase</h1>
</body>

只要extends删除该块,我的路线/网址就可以正常工作,因为此 html 呈现。

如何base.html在解决方案中正确包含来自其他应用程序的模板?

Base.html

<!DOCTYPE HTML>

{%  load staticfiles %}

{% block content %}{% endblock %}

{% include "footer.html" %}

</html>

错误:

TemplateDoesNotExist at /
templates/base.html
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 1.11.29
Exception Type: TemplateDoesNotExist
Exception Value:    
templates/base.html
Exception Location: /home/etherk1ll/.local/lib/python2.7/site-packages/django/template/engine.py in find_template, line 148
Python Executable:  /usr/bin/python
Python Version: 2.7.17
Python Path:    
['/home/etherk1ll/Development/python_projects/NewSite',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/home/etherk1ll/.local/lib/python2.7/site-packages',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages']
Server time:    Thu, 30 Apr 2020 22:30:51 +0000

标签: pythondjango

解决方案


如果你检查这个:https ://docs.djangoproject.com/en/3.0/ref/templates/builtins/#extends

它指出:

{% extends 'templates/base.html' %}

应该改为

{% extends 'base.html' %}

因为所有模板都在一个templates文件夹中。

static文件夹和模板标签中使用相同的导入逻辑


推荐阅读