在bas中引用多个块时,pythonDjango templateinheritance不起作用

2024-04-20 09:27:27 发布

您现在位置:Python中文网/ 问答频道 /正文

但我并不是很新的继承模板。我不能让页面中的所有块同时显示。不确定我是否在URL、视图或设置中丢失了某些内容。我在PyCharm上使用venv/Django 2.0.4中的python3.6

下面我的例子的细节-myhome是项目名,smarthome是app name

文件夹结构

基本.html

导航栏.html

导航侧栏.html

智能家居网址.py

智能家居视图.py

--一开始我把这个当作基本.html但根据下面线程中的建议,改为navtopbar。但不知道如何让应用程序同时显示导航边栏

设置

我遵循了this thread中的建议,但还不能使其发挥作用。谢谢你的帮助。在


Tags: 项目djangopy视图模板url内容venv
1条回答
网友
1楼 · 发布于 2024-04-20 09:27:27

首先要小心命名! 您正在navtopbar.html中呈现视图

navtopbar.html中,只有重写navtopbar块,因此只有该块将被替换。在

Djnago模板的工作原理如下:

基本.html

{% block body %} base {% endblock %}
{% block content %} base {% endblock %}

现在,如果从视图中呈现home.html,它应该是:

主页.html

^{pr2}$

如上所述,您只覆盖了一个块,这导致覆盖一个块,而另一个块保持不变。如果要重写{% block content %},则需要使用与下面相同的html重写:

主页.html

{% extends 'base.html' %}
<!  the blocks you override here only replaced  >
{% block body %}
home
{% endblock %}
{% block content %}
home content
{% endblock %}

如果您想包含来自另一个html的内容,可以使用include标记来包含它

考虑以下文件:

内容.html

<h3>This is common content</h3>

现在您可以将其包含在您的home.html中,如下所示:

主页.html

{% extends 'base.html' %}
<!  the blocks you override here only replaced  >
{% block body %}
home
{% endblock %}
{% block content %}
    {% include 'content.html' %}
{% endblock %}

相关问题 更多 >