【问题标题】:Django Templates Inclusion / BlocksDjango 模板包含/块
【发布时间】:2018-10-09 22:42:48
【问题描述】:

我有以下模板结构:

base.html:

<html>
  <head>
  </head>
  <body>
    <div class="main-content">
      {% block body %}{% endblock %}
    </div>
  </body>
</html>

detail.html 扩展 base.html:

{% extends "base.html" %}

{% block body %}
    <!-- filter side panel -->
    {% block filter_panel %}
    {% endblock %}
{% endblock %}

list.html 扩展 base.html:

{% extends "base.html" %}

{% block body %}
    <!-- filter side panel -->
    {% block filter_panel %}
    {% endblock %}
{% endblock %}   

filter.html:

{% extends "detail.html" %}
{% block filter_panel %}
  ...
  ...
{% endblock%}

现在,我已经实现了一个过滤器面板,它由一个使用onclick="filter_open()" 的按钮调用。相应的方法在外部 JS 文件中,基本上打开了一个侧面板:

function filter_open() {
  document.getElementById("filtersidebar").style.display = "block";
  document.getElementById("filtersidebar-outer").classList.add("fc-filters-overlay");
}

这个过滤器,我想在 detail.html 和 list.html 上使用。所以,我创建了一个新文件filter.htmldetail.htmllist.html 都有 {% block filter %}{% endblock %}filter.html 我在各自的 Django 块中添加功能。

但是,按钮现在会引发错误:

Uncaught TypeError: Cannot read property 'style' of null
  at filter_open (main.js:146)
  at HTMLButtonElement.onclick ((index):158)

我猜这是因为我将过滤器移到了“filter.html”。

【问题讨论】:

  • 您可以发布您的模板而不是解释它们的用途吗?
  • @Selcuk 好的,我已经编辑了原始问题!

标签: django django-templates


【解决方案1】:

从看起来你需要的是使用include标签而不是使用模板继承,即:

detail.html

{% extends "base.html" %}

{% block body %}
    <!-- filter side panel -->
    {% include "filter.html" %}
{% endblock %}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-26
    • 2016-01-19
    • 1970-01-01
    • 2012-04-17
    • 2012-03-16
    • 2020-12-12
    • 2013-06-05
    • 1970-01-01
    相关资源
    最近更新 更多