【问题标题】:Trouble with "extends" can be only used once in Django template“扩展”的问题只能在 Django 模板中使用一次
【发布时间】:2014-12-12 15:41:07
【问题描述】:

我原本以为我会使用 {% extends ["filename.html"] %} 来获取我在模板中编写的 html 并将其放入我的主文件中,但是由于您只能使用“扩展”一次,我怎样才能拥有它,以便我的 sidebar.html 和 slider.html 就像可以放入我的主 html 文件中的 sn-ps,我称之为 list.html

这是现在的样子:http://imgur.com/K5XKAcQ 这是错误的图像:http://imgur.com/Uqs3iQo

【问题讨论】:

  • 考虑使用include!
  • 你能给我举个例子吗?

标签: python django extends


【解决方案1】:

像引用基类一样使用“扩展”。不支持(直接)多重继承。要在您的内容中包含 sn-ps 代码,请使用“include”:

{% extends "layout.html" %}
{% include "sidebar.html" %}
{% include "slider.html" %}

见:https://docs.djangoproject.com/en/dev/ref/templates/builtins/

【讨论】:

    【解决方案2】:

    您可以在主模板中使用include

    而不是在sidebar.html中添加标签,在filename.html中添加:

    {% include "sidebar.html" %}
    

    【讨论】:

      【解决方案3】:

      您可以使用include 语句。另请参阅here。

      【讨论】:

        猜你喜欢
        • 2011-10-14
        • 2019-05-22
        • 1970-01-01
        • 2011-05-02
        • 2010-11-27
        • 1970-01-01
        • 2012-10-09
        • 2016-04-03
        相关资源
        最近更新 更多