【问题标题】:In Django can you have one template base layer inheriting from another?在 Django 中,你可以让一个模板基础层从另一个继承吗?
【发布时间】:2014-11-20 10:42:13
【问题描述】:

在 Django 中,你可以让一个模板基础层从另一个继承吗?

例如:

base.html

    <!DOCTYPE html>
     <html>

     {% load staticfiles %}

     <meta charset="UTF-8">
     <title>Cloud Fabric || Product Calculator</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css">

{% block additionalStyles %}

    <link rel="stylesheet" type= "text/css" href = "{% static 'fabric/editionsStylesheet.css' %}" />

    {% endblock additionalStyles %}

    </head>

    <body>

    <div id="header">
        <h1 id="HeaderH1"><i>{{EditionName}}</i></h1>
    </div>

    {% block content %}

    {% endblock content %}

    </body>
    </html>

那么这将是主要的基础层,现在有没有办法让另一个基础层从它继承来用于网站的不同部分?像这样:

base2.html

 {% extends "base.html" %}

 {% block additionalStyles %}

    {% load staticfiles %}
<link rel="stylesheet" type= "text/css" href = "{% static 'fabric/monthlyEditionsStylesheet.css' %}" />

{% endblock additionalStyles %}

{% block content %}


    <h1 id="chooseMonthH1">Choose The Monthly Range:</h1>
                                                            {% block monthlyForm %}




    {% endblock monthlyForm %}

         <button type="submit" class="pure-button pure-button-primary" id="subButton">Submit</button>

    {% block formend %}

         </form>

    {% endblock formend %}

</div>

{% endblock content %}

那么进一步的 html 可以从 base2.html 继承吗?我已经尝试过了,但是表单停止工作并且 css 搞砸了。

【问题讨论】:

    标签: html django templates inheritance django-templates


    【解决方案1】:

    您的base2.html 模板会覆盖base.html 模板中additionalStyles 块的内容。

    要保留父块的内容,您必须像这样使用神奇的block.super 变量:

    {% extends "base.html" %}
    
    {% block additionalStyles %}
        {{ block.super }}
        <link ... >
    {% endblock additionalStyles %}
    

    上述符号将内容附加到additionalStyles 块。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-24
      • 2013-08-12
      • 2012-05-19
      • 1970-01-01
      • 2020-09-10
      • 2011-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多