django - 如何补充父模板的 block ?

标签 django templates

我有first.html:

    {% block title %}
        hi there
    {% endblock %}

    {% block content %}
        blah-blah
    {% endblock %}

    {% block footer %}
        bye
    {% endblock %}

second.html。 如何将所有 first.html 页面包含到 second.html 并补充 content block ,如下所示:

hi there
blah-blah
this text is from second.html
bye

我已经尝试过

{% extends "first.html" %}
this text is from second.html

(只是没有添加任何内容)和

{% extends "first.html" %}
{% block content %}
    this text is from second.html
{% endblock %}

( block 内容被覆盖)。

最佳答案

您可以使用{{ block.super }} [Django-doc]当您也想渲染原始内容时。例如:

{% extends 'first.html' %}
{% block content %}
    <b>{{ block.super }}</b>
    this text is from second.html
{% endblock %}

关于django - 如何补充父模板的 block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58427924/

相关文章:

python - django 将 models.DecimalField 与表单结合起来 -> 错误 : quantize result has too many digits for current context

python - Django Sentry 默认标签

javascript - 在单个 Http 请求中打包多个 jQuery 模板的最佳方法?

c++ - 如何为非模板类​​定义模板方法?

django - 如何使用drf-haystack管理命令使用多个HAYSTACK_CONNECTIONS并在所有索引中获得结果?

Django 使用不正确的序列起始值为 Postgresql 创建测试数据库

c++ - 为什么这个嵌套的可变参数模板是无效参数?

javascript - 保留元素的 JS 模板引擎

python - JSON 字段 postgres 喜欢

c++ - 显式转换运算符模板的优先级和歧义