python - 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
解决方案
如果你检查这个:https ://docs.djangoproject.com/en/3.0/ref/templates/builtins/#extends
它指出:
{% extends 'templates/base.html' %}
应该改为
{% extends 'base.html' %}
因为所有模板都在一个templates
文件夹中。
在static
文件夹和模板标签中使用相同的导入逻辑
推荐阅读
- java - 保留消息的顺序 - 执行器服务 java
- reactjs - 使用redux state和axios,对象作为React子无效
- php - json数据字段和php json函数
- css - Slick Slider 仅在中心显示 1 个项目,向左和向右显示溢出
- bash - 如何将具有相同名称的fasta文件连接到一个具有不同标题的文件中?
- debugging - Visual Studio 2017 调试器关闭选择列表
- spring-boot - 在运行时编译具有对嵌套 jar 的依赖项的 Java 类
- python - 如何让文件魔术模块在 Alpine Linux 上运行?
- java - 如何让解压任务更快?
- python - 将pdf读入python的最佳实践