You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django框架中子模板内容被继承的父模板覆盖的问题求助

Django框架中子模板内容被继承的父模板覆盖的问题求助

嘿,各位Django大佬,我最近碰到个模板继承的问题,卡了好一会儿没搞懂,来这儿求帮忙!

我的问题是:我写了login.html作为子模板继承base.html,但运行后页面显示的完全是父模板里的内容,子模板的内容被覆盖了,根本看不到我在子模板里写的登录标题。

下面是我的代码:

子模板 login.html

{% extends "./base.html" %}

{% block content %}

<h2>Tryin to Log In</h2>

{% endblock content %}

父模板 base.html

{% block content %}

<h2>base template</h2>

{% endblock %}

settings.py 中的模板配置

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / "templates"],
        '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',
            ],
        },
    },
]

我本来以为子模板里的content块会替换父模板的同名块,但实际情况是父模板的内容完全覆盖了子模板的。我检查了模板路径的配置,DIRS指向了项目根目录下的templates文件夹,APP_DIRS也设为了True,看起来没什么问题,有没有人能帮我看看哪里出错了呀?

备注:内容来源于stack exchange,提问作者user5539903

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.23 08:23:10