【问题标题】:Django include template no dataDjango包含模板没有数据
【发布时间】:2022-01-20 18:47:18
【问题描述】:

我正在尝试在我的 base.html 中包含一个文件 基地内部,客栈base.html

{% block content %}

{% include "something.html" %}
{% endblock %}

但是包含的文件中没有显示任何内容,我试图在 something.html 中设置块

{% block content %}
something something inside the something.html
{% endblock %}

我也试过不使用积木,但没有。

【问题讨论】:

  • 您忘记了 {% include "something.html" %} 标记中的右双引号 (")。
  • 更新了,我只是在问题中忘记了,不在模板中
  • 您的“父”模板是否定义了content 块?
  • 所有其他扩展基础的模板都有块内容是
  • 并且你没有在something.html中使用{% block ... %}s等。

标签: python django django-templates


【解决方案1】:

你想做的是

base.html

{% block content %}

{% include "something.html" %}

{% endblock %}

something.html

<div>my content in something</div>

如果您将 block 语句放入包含的模板中,它将不会显示。

更多详情https://docs.djangoproject.com/en/4.0/ref/templates/builtins/#include

【讨论】:

  • 我试过有没有,还是没有出现
  • 他们在同一个目录吗? include 语句是相对于您的 TEMPLATE_ROOT
【解决方案2】:

whatyouwanttoinclude.html

<body>
............
    {% block content %}
    {% endblock content %}
...........
</body>

whereyouwanttoinclude.html

{% extends 'whatyouwanttoinclude.html' %}
{% block content %}
    some html here
{% endblock content %}

现在,当您渲染 whereyouwanttoinclude.html 模板时,它也会显示 whatyouwanttoinclude.html 的内容

【讨论】:

    猜你喜欢
    • 2013-06-17
    • 2014-05-29
    • 2012-03-16
    • 1970-01-01
    • 2018-01-14
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 2017-12-20
    相关资源
    最近更新 更多