django - <title> {% block title %} Home {% endblock %} </title> 没有被其他页面覆盖?

标签 django django-templates

这是我的基础文件

{% load static %}
{% include "feed/header.html" %}
{% block content%}

{% endblock %}
{% include "feed/footer.html" %}

这是我的标题,其中包括标题:

{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
     <title>
        {% block title %} Home {% endblock %}
     </title>

因此,当我尝试在我的详细信息页面中覆盖标题时,它始终显示标题的标题,即主页(不更改)而不是我覆盖的详细信息页面的标题。以下是我在详细页面中使用的代码:

{% extends 'feed/base.html'%}
{% block title %} Details {% endblock %}

{% block content %}

some views
{% endblock %}

所以请帮我解决这个问题。

最佳答案

作为 documentation说,您只能“覆盖”在您直接或间接扩展的模板中定义的 block (即,您从扩展自身的模板扩展):

The include tag should be considered as an implementation of “render this subtemplate and include the HTML”, not as “parse this subtemplate and include its contents as if it were part of the parent”. This means that there is no shared state between included templates – each include is a completely independent rendering process.

Blocks are evaluated before they are included. This means that a template that includes blocks from another will contain blocks that have already been evaluated and rendered - not blocks that can be overridden by, for example, an extending template.

因此,您需要将 block 内联到父模板中。例如:

{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
     <title>
        <b>{% block title %} Home {% endblock %}</b>
     </title>
</head>
{% block content%}

{% endblock %}
{% include "feed/footer.html" %}

然后在“子模板”中覆盖它。

关于django - &lt;title&gt; {% block title %} Home {% endblock %} &lt;/title&gt; 没有被其他页面覆盖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52211192/

相关文章:

javascript - 如何在 Django 模板过滤器中使用 Sekizai javascript block ?

python - 同一站点下的多个 Django 应用程序

python - 如何为表单中未填写的字段添加错误消息?

python - 如何在 django 中定义 json 响应的嵌套列表

django:如何仅访问 form.errors 中的第一条错误消息?

python - 整数字段前缀零不显示

python - django Rest框架如何删除多余的查询?

html - Css 为什么我的 <a> 标签不在我的 "div"中间。 valign=middle 不起作用

django - Django中如何获取前N个元素?

python - 基于 Forloop 模式的图像大小