Django:包含来自其他应用程序的模板

时间:2013-09-25 16:12:36

标签: django django-models django-forms django-templates django-views

我有一个项目应用和一个文件应用。在 project.html 文件中,我想要包含 file_list.html 文件。这样所有与项目相对应的文件都显示在项目页面上。

project.html (项目应用)

{% extends "site_base.html" %}
...
{% block body %}
...
<div id="files">
    <p>More here soon but in the meantime, here's a link to the
        <a href="{% groupurl file_list project %}">file list</a>
    </p>
</div>
...
{% endblock body %}

我想直接插入文件列表,而不是链接到文件列表。

file_list.html (文件应用)

{% extends "site_base.html" %}
{% block body %}

{% for file in files %}
<tr>
    <td>{{ file.filename }}</td>
</tr>
{% endfor %}

{% endblock body %}

我提出了以下可能性,但我不确定实际工作和最佳实践

  • 包含{%include%}的file_list.html。但是我错过了文件的上下文
  • 撰写inclus_tag
  • 使用{%block%}
  • 使用模板加载器

在这种情况下,最佳解决方案是什么?还有其他可能性吗?

1 个答案:

答案 0 :(得分:3)

包含标记是唯一可能的答案,因为其他任何一个都不会包含模板上下文中的实际文件列表。这正是包含标记的用途。