【问题标题】:extend base.html problem扩展 base.html 问题
【发布时间】:2010-06-17 23:48:26
【问题描述】:

我收到以下错误:

Template error

In template /home/mo/python/django/templates/yoga/index.html, error at line 1
Caught TemplateDoesNotExist while rendering: base.html
1 {% extends "base.html" %}
2 
3 {% block main %}
4     <p>{{ page.title }}</p>
5     <p>{{ page.info}}</p>
6     <a href="method/">Method</a>
7 {% endblock %}
8 

这是我的 base.html 文件,与 index.html 位于同一位置

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <div style="width:50%; marginleft:25%;">
    {% block main %}{% endblock %}
    </div>

这里到底发生了什么? base.html 文件应该位于其他地方吗?

【问题讨论】:

    标签: django templates extends


    【解决方案1】:

    yoga/index.html“在同一个地方”表示模板实际上是yoga/base.html

    【讨论】:

      【解决方案2】:

      假设 TEMPLATE_DIRS 包含/home/mo/python/django/templates/,没有.../yoga/,所以你需要:

      base.html 移动到/home/mo/python/django/templates/

      在你的模板中写上{% extends "yoga/base.html" %}

      【讨论】:

      • 在这两个修复选项中,我认为最好的做法是将base.html 放在/home/mo/python/django/templates/ 中并保持您对它的extends 引用不变。这将使应用程序保持模块化。您构建的另一个站点可以选择使用您的应用程序的base.html 或覆盖它并强制您的应用程序采用新站点的新页眉/页脚/样式。我认为这就是 Pinax 做主题等的方式。
      【解决方案3】:

      base.html 是否位于设置文件的 TEMPLATE_DIRS 列表中的目录中?

      【讨论】:

        【解决方案4】:

        Base.html 文件必须位于模板文件夹的底部才能使用'base.html',如果位于另一个文件夹中,例如:templates / home / base.html,则必须使用指定路径

        { % Extends ' home / base.html '% }
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-27
        • 1970-01-01
        • 2021-02-05
        • 1970-01-01
        • 2011-09-17
        • 1970-01-01
        • 2020-11-15
        • 1970-01-01
        相关资源
        最近更新 更多