首页 > 解决方案 > Django:“扩展”在模板中不起作用

问题描述

我遵循了最新的 Django 文档,使用了{% extends %}与他们在示例中相同的方式使用关键字,但我无法让它工作。我究竟做错了什么?

base.html:下面定义的contenttransaction_detail.html工作:

<div class="row">
        <div class="col-md-8">
          {% if messages %}
          {% endif %}
          {% block content %}{% endblock %} <!--This block defined in transaction_detail.html is displaying correctly -->
        </div>
...

transaction_detail.html:注意test区块包含在content区块内。这是允许的吗?

{% extends 'blog/base.html' %}
{% block content %} <!-- This block displays correctly -->

 <div class="content-section">
            <!-- Chat -->
            {% block test %} <!-- This block does not display at all -->
            
            {% endblock %}
<div>
...

房间.html:

{% extends 'blog/transaction_detail.html' %}

    {% block test %}
        <div class="messages">
            <ul id="chat-log">
            </ul>
            <div class="message-input">
                <div class="wrap">
                    <input id="chat-message-input" type="text" placeholder="Write your message..." />
                    <button id="chat-message-submit" class="submit">
                    <i class="fa fa-paper-plane" aria-hidden="true"></i>
                    </button>
                </div>
            </div>
        </div>
    {% endblock %}

settings.py(仅限 TEMPLATES 变量):我列出了所有应用程序INSTALLED_APPS

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        #DIRS: a list of directories where the engine should look for template source files, in search order.
        'DIRS': [os.path.join(BASE_DIR, ''), # root directory
                 'django_project/blog/templates'], # templates in blog app
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

谁能看到我哪里出错了?我想我在 transaction_detail.html/room.html 父/子关系中遵循与 base.html/transaction_detail.html 相同的父/子关系。

标签: djangodjango-templates

解决方案


推荐阅读