【问题标题】:Django app not including base.html template in downstream appsDjango 应用程序不包括下游应用程序中的 base.html 模板
【发布时间】:2020-04-30 22:33:59
【问题描述】:

我有一个 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

【问题讨论】:

    标签: python django


    【解决方案1】:

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

    它指出:

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

    应该改为

    {% extends 'base.html' %}
    

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

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

    【讨论】:

      猜你喜欢
      • 2014-09-13
      • 2013-03-30
      • 2016-02-10
      • 2023-03-07
      • 1970-01-01
      • 2013-06-13
      • 2012-09-17
      • 2017-03-30
      • 2013-06-04
      相关资源
      最近更新 更多