2

Have a website that I have 1 div black rectangle used a as footer, data are pulled from DB inside the footer.

I would like to show this footer on every template that I have created in the django template directory except INDEX.HTML main page (template). If I put it into the BASE.HTML it will be everywhere, if I spread it to all my 20 templates except INDEX.HTML I would need to edit every each template for data in the footer I dont want to do that. How can I achieve footer is showing in all templates except INDEX.HTML but using it only once for editing data in the footer. Is there a django command or something like IF template = index.html hide the footer etc similar logic or what is the best way to achieve this?

Thanks

4

1 回答 1

1

您可以为此目的使用block标签。

如果您发现自己在多个模板中复制了内容,这可能意味着您应该将该内容移动到{% block %}父模板中的

更多文档在这里

base.html

{% block footer_content %}
    {# contents here #}
{% endblock %}

并且在index.html

{% block footer_content %}
    {# this overwrites the base footer_content to empty block #}
{% endblock %}

这将删除仅在index.html继承自的所有其他页面中可用的内容base.html

现在,如果您想向某些页面添加更多内容,

{% block footer_content %}
    {{ block.super }}
    {# more content here #}
{% endblock %}
于 2013-09-11T19:57:20.613 回答