【问题标题】:i should inheritance in django template html file我应该在 django 模板 html 文件中继承
【发布时间】:2021-07-10 07:32:15
【问题描述】:

我有两个 html 文件,我应该从第一个文件扩展第二个文件,但它不起作用

第一个文件base.html

<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>{% block title %}Library{% endblock %}</title>
</head>

<body>
    <div id="test">
        {% block test %}
            <h1>This page belongs to me.</h1>
        {% endblock %}
    </div>
</body>
</html>

第二个 booklist.html

{% extends "base.html" %}

{% block content %}
<h3>Books</h3>
<div>
{% for book in books %}
    {% if book.available %}
        <p>{{ book.author }} wrote {{ book.title }}.</p>
    {% endif %}
{% endfor %}
</div>
{% endblock %}

【问题讨论】:

    标签: html django-views django-forms django-templates


    【解决方案1】:

    您忘记添加父模板

    {% block content %}{% endblock %}
    

    这是放置子模板的地方。 您的父模板应如下所示(例如):

    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <title>{% block title %}Library{% endblock %}</title>
    </head>
    
    <body>
        <div id="test">
            {% block test %}
                <h1>This page belongs to me.</h1>
            {% endblock %}
        </div>
    
    <div class="myChildContentComesHere">{% block content %}{% endblock %}</div>
    </body>
    
    </html>
    

    【讨论】:

      猜你喜欢
      • 2015-11-13
      • 2011-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-23
      • 2013-01-09
      • 2011-12-24
      相关资源
      最近更新 更多