这是我的基础文件
{% 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 - <title> {% block title %} Home {% endblock %} </title> 没有被其他页面覆盖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52211192/