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

问题描述:

在Django中,你可以有一个从另一个继承的模板基础层吗?在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混乱了。

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

为了保护父母的块的内容,你必须使用魔法block.super变量是这样的:

{% extends "base.html" %} 

{% block additionalStyles %} 
    {{ block.super }} 
    <link ... > 
{% endblock additionalStyles %} 

以上符号追加内容到additionalStyles块。