我有一个继承基本模板的 Django (1.6) 应用程序。我想将我的一个(当前工作的)平面页面包含到应用程序登录页面中,Django docs say is possible .
这是我的模板:
{% extends "path/to/base.html" %}
{% load flatpages %}
{% get_flatpages as fp %}
{% block content %}
<h3>Flatpage inclusion</h3>
<p>Number of flatpages: {{ fp|length }}
<ul>
{% for page in fp %}
<li><a href="{{ page.url }}">{{ page.title }}</a></li>
{% endfor %}
</ul>
{% endblock content %}
这没有列出任何平面图。但是,如果我删除 {% extends %} 信号,那么我的代码如下所示:
{% load flatpages %}
{% get_flatpages as fp %}
<h3>Flatpage inclusion</h3>
<p>Number of flatpages: {{ fp|length }}
<ul>
{% for page in fp %}
<li><a href="{{ page.url }}">{{ page.title }}</a></li>
{% endfor %}
</ul>
一切正常。我在我的 fp 对象中看到平面页面的数量 (9),我的无序列表显示了所有平面页面的 url 和标题。
在我看来,这似乎是平面页面工作方式或 Django 模板继承方式中的一个错误。
基本模板 (/path/to/base.html) 中没有任何复杂的内容。
Django categorically says that this is possible :
When you load a custom tag or filter library, the tags/filters are only made available to the current template – not any parent or child templates along the template-inheritance path.
For example, if a template foo.html has {% load humanize %}, a child template (e.g., one that has {% extends "foo.html" %}) will not have access to the humanize template tags and filters. The child template is responsible for its own {% load humanize %}.
This is a feature for the sake of maintainability and sanity.
有没有其他人注意到这个错误?是否仅内置平面应用程序异常(exception)?
编辑 1:
丹尼尔斯的回答是正确的。 example code from the Django docs不显示包含 {% block %} 中的平面内容,所以我没想到需要这样做:
{% load flatpages %}
{% get_flatpages as flatpages %}
<ul>
{% for page in flatpages %}
<li><a href="{{ page.url }}">{{ page.title }}</a></li>
{% endfor %}
</ul>
我猜是我的错。生活和学习。
请您参考如下方法:
问题是您的 get_flatpages 标记位于父模板的任何 block 之外。这意味着它根本不会被调用。
将它移到内容 block 中,它应该可以工作。
