python - 将模板分成几个部分并包含每个部分不好吗?

标签 python django django-templates

我有一个基本模板,我想将其分成三个部分:页眉、正文和页脚。然后我使用基本模板来包含三个子模板。但是,据我所见,这意味着我无法覆盖 {{ block }} 内容。那么使用 includes 是个坏主意吗?或者有没有办法覆盖包含的模板中的 block 内容?

我知道您可以将静态上下文变量发送到包含的段,但它需要更加动态。

我的代码:

在 header.html 中

<html>
    <head>
        <script url="..."></script>
        <link rel="..." />
        {% block head_extension %}

        {% endblock %}
    </head>
    <body>
        <header>
            <div class="headerstuff">
            </div>
        </header>

然后在body.html文件中:

        <div class="container">
            {% block content %}
                Foo fum my content    
            {% endblock %}
        </div>

页脚.html:

        <footer>
            {% block footer %}
                Copyright 2015
            {% endblock %}
        </footer>
    </body>
</html>

基础.html:

{% include "header.html" %}
{% include "body.html" %}
{% include "footer.html" %}
<!-- and the part that doesn't work -->
{% block head_extension %}
    <script src="unique_script"></script>
{% endblock %}
{% block content %}
    My unique content
{% endblock %}
{% block footer %}
    Copyright 2011
{% endblock %}
<!-- end broken django templating try -->

我做错了什么吗?模板文档似乎表明我正在尝试做的事情不起作用。这似乎是创建易于阅读的模板的最佳方式。将所有部分放在一个大文件中是否更好?可以想象,页眉、正文和页脚元素比这个演示大得多。但重点仍然存在。

我希望有一种方法可以做我想做但我不知道的事情。

提前致谢

最佳答案

您的方法很好,但是您按错误的顺序执行此操作。首先 html 开始 <html>和结束标记 </html>不应该分成不同的文件,最好放在base.html中.

以下是如何遵循分解结构的示例:

base.html

<html>
    <head>
        <!-- Some stuff here which should be included in all templates for example js or css -->
        {% block extra_css %}
            <!-- to included app-template dependent css -->
        {% endblock extra_css %}

        {% block extra_js %}
            <!-- to included app-template dependent js -->
        {% endblock extra_js %}

        {% block extra_head %}
            <!-- for anything else inside head -->
        {% endblock extra_head %}

    </head>
    <body>
        {% block menu %}
            <!-- Default menu here -->
            {% block extra_menu %}
                <!-- extend menu based on template -->
            {% endblock extra_menu %}
        {% endblock menu %}

        {% block content %}
            <div>This is good</div>
        {% endblock content %}

        {% include "footer.html" %}

        {% block bottom_js %}
            <!-- if you want to have manual js scripts at bottom -->
        {% endblock bottom_js %}
    </body>
</html>

现在base.html现在所有设置都让我们假设您要从另一个子模板覆盖 base.html block content你会做:

child.html

{% extends "base.html" %}

{% block content %}
    <div>This is really good</div>
{% endblock content %}

所以当页面加载时你会看到this is really good而不是 this is good (这是在内容 block 内的 base.html 中定义的)因为您只是覆盖了它。

如果你想要 base.html 中内容 block 中的任何内容也应该保留然后你需要扩展 block 而不是使用方法完全覆盖它 {{ block.super }}

child.html

{% extends "base.html" %}

{% block content %}
    {{ block.super }}
    <div>This is really good</div>
{% endblock content %}

现在您将同时看到 this is goodthis is really good .希望这会澄清您的概念并引导您。

关于python - 将模板分成几个部分并包含每个部分不好吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16498176/

相关文章:

python - 如何解决 strptime() 引起的 naive datetime RuntimeWarning?

django - 如何个性化 django 管理模板?

django - 模板中的多对多项目 : check if any are not empty or none

python - 在 DJANGO 模板中渲染多个谷歌地图标记的最佳方法是什么

python - 创建独特的单败淘汰赛的方法

python - 在 SQLite 中插入日期

python - Django 模型表单集 : only track changes to those items that have been updated/saved in the set?

python - 按键名合并字典值

python - 英雄联盟。 New Relic Procfile 命令不起作用

Django:安全中间件使站点崩溃